WPF TextBox和PasswordBox添加水印
本文實(shí)例為大家分享TextBox和PasswordBox加水印的方法,供大家參考,具體內(nèi)容如下
Textbox加水印
Textbox加水印,需要一個(gè)VisualBrush和觸發(fā)器驗(yàn)證Text是否為空,在空的時(shí)候設(shè)置背景的Brush就可以實(shí)現(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加水印,需要添加判斷輸入非空的依賴屬性,因?yàn)镻asswordBox本身沒(méi)有這個(gè)屬性。
通過(guò)一個(gè)PasswordLength函數(shù)判斷密碼框的長(zhǎng)度是不是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暴露出來(lái),做一個(gè)可以修改水印的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)用只有一句話
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#WinForm實(shí)現(xiàn)多語(yǔ)言切換的示例
本文主要介紹了C#WinForm實(shí)現(xiàn)多語(yǔ)言切換的示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01C#實(shí)現(xiàn)將PDF轉(zhuǎn)為線性化PDF
線性化PDF文件是PDF文件的一種特殊格式,可以通過(guò)Internet更快地進(jìn)行查看。這篇文章主要介紹了如何通過(guò)C#實(shí)現(xiàn)將PDF轉(zhuǎn)為線性化PDF,感興趣的小伙伴可以學(xué)習(xí)一下2021-12-12c# 對(duì)cookies(增、刪、改、查)的操作方法
以前覺(jué)得cookies操作無(wú)非就那么幾種,但是“杯具事件”還是很多的,下面分享一下對(duì)cookies的簡(jiǎn)單操作2013-04-04C# 檢索不區(qū)分大小寫(xiě)并高亮顯示實(shí)例詳解
這篇文章主要介紹了C# 檢索不區(qū)分大小寫(xiě)并高亮顯示實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-01-01Unity3D開(kāi)發(fā)教程:憤怒的小鳥(niǎo)
這篇文章詳細(xì)的講解了如何從0開(kāi)發(fā)出一個(gè)Unity3D的小游戲憤怒的小鳥(niǎo),本文包含大量的圖片與文字描述,也含有大量的源代碼,可以讓你快速入手,希望本篇文章對(duì)你有所幫助2021-06-06C#面向?qū)ο缶幊讨氯螒驅(qū)崿F(xiàn)方法
這篇文章主要介紹了C#面向?qū)ο缶幊讨氯螒驅(qū)崿F(xiàn)方法,以一個(gè)完整的猜拳游戲?yàn)槔v述了C#面向?qū)ο蟪绦蛟O(shè)計(jì)的具體實(shí)現(xiàn)步驟,具有一定的學(xué)習(xí)與借鑒價(jià)值,需要的朋友可以參考下2014-11-11