亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

WPF TextBox和PasswordBox添加水印

 更新時間:2016年11月11日 17:14:26   作者:眾尋  
這篇文章主要為大家詳細介紹了WPF TextBox和PasswordBox添加水印的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享TextBox和PasswordBox加水印的方法,供大家參考,具體內(nèi)容如下

Textbox加水印

Textbox加水印,需要一個VisualBrush和觸發(fā)器驗證Text是否為空,在空的時候設置背景的Brush就可以實現(xiàn)水印效果。

<TextBox Name="txtBoxName" Width="120" Height="23">
      <TextBox.Resources>
        <VisualBrush x:Key="HelpBrush" TileMode="None" Opacity="0.3" Stretch="None" AlignmentX="Left">
          <VisualBrush.Visual>
            <TextBlock FontStyle="Italic" Text="水印效果"/>
          </VisualBrush.Visual>
        </VisualBrush>
      </TextBox.Resources>
      <TextBox.Style>
        <Style TargetType="TextBox">
          <Setter Property="Height" Value="23"/>
          <Setter Property="HorizontalAlignment" Value="Left"/>
          <Setter Property="VerticalAlignment" Value="Top"/>
          <Style.Triggers>
            <Trigger Property="Text" Value="{x:Null}">
              <Setter Property="Background" Value="{StaticResource HelpBrush}"/>
            </Trigger>
            <Trigger Property="Text" Value="">
              <Setter Property="Background" Value="{StaticResource HelpBrush}"/>
            </Trigger>
          </Style.Triggers>
        </Style>
      </TextBox.Style>
    </TextBox>

PasswordBox加水印

PasswordBox加水印,需要添加判斷輸入非空的依賴屬性,因為PasswordBox本身沒有這個屬性。

通過一個PasswordLength函數(shù)判斷密碼框的長度是不是0,如果是0則顯示背景水印,否則就隱藏。

屬性部分代碼,CS文件

public class PasswordBoxMonitor : DependencyObject
  {
    public static bool GetIsMonitoring(DependencyObject obj)
    {
      return (bool)obj.GetValue(IsMonitoringProperty);
    }

    public static void SetIsMonitoring(DependencyObject obj, bool value)
    {
      obj.SetValue(IsMonitoringProperty, value);
    }

    public static readonly DependencyProperty IsMonitoringProperty =
      DependencyProperty.RegisterAttached("IsMonitoring", typeof(bool), typeof(PasswordBoxMonitor), new UIPropertyMetadata(false, OnIsMonitoringChanged));



    public static int GetPasswordLength(DependencyObject obj)
    {
      return (int)obj.GetValue(PasswordLengthProperty);
    }

    public static void SetPasswordLength(DependencyObject obj, int value)
    {
      obj.SetValue(PasswordLengthProperty, value);
    }

    public static readonly DependencyProperty PasswordLengthProperty =
      DependencyProperty.RegisterAttached("PasswordLength", typeof(int), typeof(PasswordBoxMonitor), new UIPropertyMetadata(0));

    private static void OnIsMonitoringChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
      var pb = d as PasswordBox;
      if (pb == null)
      {
        return;
      }
      if ((bool)e.NewValue)
      {
        pb.PasswordChanged += PasswordChanged;
      }
      else
      {
        pb.PasswordChanged -= PasswordChanged;
      }
    }

    static void PasswordChanged(object sender, RoutedEventArgs e)
    {
      var pb = sender as PasswordBox;
      if (pb == null)
      {
        return;
      }
      SetPasswordLength(pb, pb.Password.Length);
    }
  }

XMAL代碼

<PasswordBox Name="pb" Width="120" VerticalAlignment="Bottom" Height="35">
      <PasswordBox.Style>
        <Style TargetType="PasswordBox">
          <Setter Property="Height" Value="23"/>
          <Setter Property="HorizontalAlignment" Value="Left"/>
          <Setter Property="VerticalAlignment" Value="Top"/>
          <Setter Property="local:PasswordBoxMonitor.IsMonitoring" Value="True"/>
          <Setter Property="Template">
            <Setter.Value>
              <ControlTemplate TargetType="{x:Type PasswordBox}">
                <Border Name="Bd" Background="{TemplateBinding Background}" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" SnapsToDevicePixels="True">
                  <Grid>
                    <ScrollViewer x:Name="PART_ContentHost" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                    <StackPanel Orientation="Horizontal" Visibility="Collapsed" Name="myStackPanel">
                      <TextBlock HorizontalAlignment="Left" VerticalAlignment="Center" Foreground="LightGray" Text="水印效果"/>
                    </StackPanel>
                  </Grid>
                </Border>
                <ControlTemplate.Triggers>
                  <Trigger Property="IsEnabled" Value="false">
                    <Setter Property="Visibility" TargetName="myStackPanel" Value="Collapsed"/>
                  </Trigger>
                  <Trigger Property="local:PasswordBoxMonitor.PasswordLength" Value="0">
                    <Setter Property="Visibility" TargetName="myStackPanel" Value="Visible"/>
                  </Trigger>
                </ControlTemplate.Triggers>
              </ControlTemplate>
            </Setter.Value>
          </Setter>
        </Style>
      </PasswordBox.Style>
    </PasswordBox>


效果圖

2016-09-07 新增內(nèi)容

將TextBlock暴露出來,做一個可以修改水印的Textbox控件

<TextBox x:Class="OracleCodeGenerator.watermarkTextBox"
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
       xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
       xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
       xmlns:local="clr-namespace:OracleCodeGenerator"
       mc:Ignorable="d" 
       d:DesignHeight="300" d:DesignWidth="300" Name="tb">
  <TextBox.Resources>
    <VisualBrush x:Key="HelpBrush" TileMode="None" Opacity="0.3" Stretch="None" AlignmentX="Left">
      <VisualBrush.Visual>
        <TextBlock Text="{Binding TbText,ElementName=tb}" FontStyle="Italic"/>
      </VisualBrush.Visual>
    </VisualBrush>
  </TextBox.Resources>
  <TextBox.Style>
    <Style TargetType="TextBox">
      <Setter Property="Height" Value="23"/>
      <Setter Property="HorizontalAlignment" Value="Left"/>
      <Setter Property="VerticalAlignment" Value="Top"/>
      <Style.Triggers>
        <Trigger Property="Text" Value="{x:Null}">
          <Setter Property="Background" Value="{StaticResource HelpBrush}"/>
        </Trigger>
        <Trigger Property="Text" Value="">
          <Setter Property="Background" Value="{StaticResource HelpBrush}"/>
        </Trigger>
      </Style.Triggers>
    </Style>
  </TextBox.Style>
</TextBox>
public partial class watermarkTextBox : TextBox
  {
    public watermarkTextBox()
    {
      InitializeComponent();
    }

    private string tbText;

    public string TbText
    {
      get
      {
        return tbText;
      }

      set
      {
        tbText = value;
      }
    }
  }

調(diào)用只有一句話

復制代碼 代碼如下:
<local:watermarkTextBox Width="150" TbText="我是水印"/>

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • C#WinForm實現(xiàn)多語言切換的示例

    C#WinForm實現(xiàn)多語言切換的示例

    本文主要介紹了C#WinForm實現(xiàn)多語言切換的示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-01-01
  • C#實現(xiàn)將PDF轉為線性化PDF

    C#實現(xiàn)將PDF轉為線性化PDF

    線性化PDF文件是PDF文件的一種特殊格式,可以通過Internet更快地進行查看。這篇文章主要介紹了如何通過C#實現(xiàn)將PDF轉為線性化PDF,感興趣的小伙伴可以學習一下
    2021-12-12
  • KMP算法的C#實現(xiàn)方法

    KMP算法的C#實現(xiàn)方法

    這篇文章主要介紹了KMP算法的C#實現(xiàn)方法,代碼簡潔實用,需要的朋友可以參考下
    2014-09-09
  • c# 對cookies(增、刪、改、查)的操作方法

    c# 對cookies(增、刪、改、查)的操作方法

    以前覺得cookies操作無非就那么幾種,但是“杯具事件”還是很多的,下面分享一下對cookies的簡單操作
    2013-04-04
  • C#中List和數(shù)組之間轉換的方法

    C#中List和數(shù)組之間轉換的方法

    這篇文章主要介紹了C#中List和數(shù)組之間轉換的方法,涉及比較簡單的轉換技巧,需要的朋友可以參考下
    2015-02-02
  • C# 檢索不區(qū)分大小寫并高亮顯示實例詳解

    C# 檢索不區(qū)分大小寫并高亮顯示實例詳解

    這篇文章主要介紹了C# 檢索不區(qū)分大小寫并高亮顯示實例詳解的相關資料,需要的朋友可以參考下
    2017-01-01
  • Unity3D開發(fā)教程:憤怒的小鳥

    Unity3D開發(fā)教程:憤怒的小鳥

    這篇文章詳細的講解了如何從0開發(fā)出一個Unity3D的小游戲憤怒的小鳥,本文包含大量的圖片與文字描述,也含有大量的源代碼,可以讓你快速入手,希望本篇文章對你有所幫助
    2021-06-06
  • C#之字符串截取--Regex.Match使用

    C#之字符串截取--Regex.Match使用

    這篇文章主要介紹了C#之字符串截取--Regex.Match使用解讀,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • c#多種加解密示例(md5加密解密)

    c#多種加解密示例(md5加密解密)

    這篇文章主要介紹了c#多種加解密示例,包括了MD5加密,SHA1加密,DES加解密,需要的朋友可以參考下
    2014-03-03
  • C#面向對象編程之猜拳游戲實現(xiàn)方法

    C#面向對象編程之猜拳游戲實現(xiàn)方法

    這篇文章主要介紹了C#面向對象編程之猜拳游戲實現(xiàn)方法,以一個完整的猜拳游戲為例講述了C#面向對象程序設計的具體實現(xiàn)步驟,具有一定的學習與借鑒價值,需要的朋友可以參考下
    2014-11-11

最新評論