最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

WPF TextBox和PasswordBox添加水印

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

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

Textbox加水印

Textbox加水印,需要一個VisualBrush和觸發(fā)器驗證Text是否為空,在空的時候設(shè)置背景的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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 詳解C#中Dictionary<TKey,TValue>的存儲結(jié)構(gòu)

    詳解C#中Dictionary<TKey,TValue>的存儲結(jié)構(gòu)

    無論是實際的項目中,還是在我們學習的過程中,都會重點的應用到Dictionary<TKey,?TValue>這個存儲類型,所以本文就來為大家介紹一下這一存儲結(jié)構(gòu)的相關(guān)知識,希望對大家有所幫助
    2023-11-11
  • C#自定義處理xml數(shù)據(jù)類實例

    C#自定義處理xml數(shù)據(jù)類實例

    這篇文章主要介紹了C#自定義處理xml數(shù)據(jù)類,涉及C#針對XML的打開、讀寫等常用操作,并將其封裝進一個類中以便于調(diào)用,非常具有實用價值,需要的朋友可以參考下
    2015-03-03
  • C#冒泡法排序算法實例分析

    C#冒泡法排序算法實例分析

    這篇文章主要介紹了C#冒泡法排序算法,結(jié)合兩個常用實例分析了C#冒泡排序算法的相關(guān)實現(xiàn)技巧,需要的朋友可以參考下
    2015-07-07
  • C#調(diào)用SQL語句時乘號的用法

    C#調(diào)用SQL語句時乘號的用法

    這篇文章主要介紹了C#調(diào)用SQL語句時乘號的用法,可避免因符號引起的程序錯誤,是C#程序設(shè)計人員有必要掌握的,需要的朋友可以參考下
    2014-08-08
  • C#實現(xiàn)利用Windows API讀寫INI文件的方法

    C#實現(xiàn)利用Windows API讀寫INI文件的方法

    這篇文章主要介紹了C#實現(xiàn)利用Windows API讀寫INI文件的方法,涉及C#針對ini文件的創(chuàng)建、讀取及寫入等操作技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-07-07
  • C#實現(xiàn)觀察者模式(Observer?Pattern)的兩種方式

    C#實現(xiàn)觀察者模式(Observer?Pattern)的兩種方式

    這篇文章介紹了C#實現(xiàn)觀察者模式(Observer?Pattern)的兩種方式,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-08-08
  • c# 實現(xiàn)子窗口關(guān)閉父窗口也關(guān)閉的簡單實例

    c# 實現(xiàn)子窗口關(guān)閉父窗口也關(guān)閉的簡單實例

    下面小編就為大家?guī)硪黄猚# 實現(xiàn)子窗口關(guān)閉父窗口也關(guān)閉的簡單實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-02-02
  • C# 并行和多線程編程——Task進階知識

    C# 并行和多線程編程——Task進階知識

    這篇文章主要介紹了C# 并行和多線程編程——Task進階知識的的相關(guān)資料,幫助大家更好的理解和學習使用c#,感興趣的朋友可以了解下
    2021-02-02
  • WPF實現(xiàn)控件拖動的示例代碼

    WPF實現(xiàn)控件拖動的示例代碼

    這篇文章主要介紹了WPF實現(xiàn)控件拖動的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-08-08
  • Unity3d實現(xiàn)Flappy Bird游戲

    Unity3d實現(xiàn)Flappy Bird游戲

    這篇文章主要為大家詳細介紹了Unity3d實現(xiàn)Flappy Bird游戲,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-12-12

最新評論

志丹县| 崇左市| 鄂温| 鄢陵县| 沐川县| 沈丘县| 蕉岭县| 汝城县| 阿拉善盟| 措美县| 青岛市| 龙州县| 拉萨市| 汝南县| 宜春市| 襄城县| 黎川县| 元阳县| 瑞安市| 延长县| 惠水县| 和静县| 堆龙德庆县| 海阳市| 密山市| 张家界市| 蒙阴县| 资中县| 陇西县| 平乡县| 孝义市| 辽宁省| 怀来县| 卓资县| 黑龙江省| 达拉特旗| 仁布县| 永平县| 凤冈县| 兴宁市| 教育|