[Convert] Add a watermark to WPF TextBox and PasswordBox, and wpfpasswordbox
Address: http://www.w2bc.com/Article/14488
Textbox watermark
To add a watermark to Textbox, you need a VisualBrush and trigger to verify whether the Text is empty. When it is empty, you can set a background Brush to achieve the watermark effect.
<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 = "watermark effect"/> </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>
Add a watermark to PasswordBox
When adding a watermark to PasswordBox, you must add a dependency attribute that determines whether the input is not empty, because PasswordBox itself does not have this attribute.
Use a PasswordLength function to determine whether the password frame length is 0. If it is 0, the background watermark is displayed. Otherwise, the password is hidden.
Attribute code, CS File
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 code
<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} "required =" True "> <Grid> <ScrollViewer x: name = "PART_ContentHost" placement = "{TemplateBinding SnapsToDevicePixels}"/> <StackPanel Orientation = "Horizontal" Visibility = "Collapsed" Name = "myStackPanel"> <TextBlock placement = "Left" verticalAlignment = "Center" Foreground = "LightGray" Text = "watermark effect"/> </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>