WPF 密码提示框
首先展示效果
如图密码提示框,可通过小眼睛进行显示和隐藏密码
1、自定义控件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | public class TitlePasswordBox : TextBox { /// <summary> /// 密码提示框提示语 /// </summary> public string Title { get { return ( string )GetValue(TitleProperty); } set { SetValue(TitleProperty, value); } } public static readonly DependencyProperty TitleProperty = DependencyProperty.Register( "Title" , typeof ( string ), typeof (TitlePasswordBox), new PropertyMetadata( "" )); /// <summary> /// 是否显示密码 /// </summary> public bool IsDisplay { get { return ( bool )GetValue(IsDisplayProperty); } set { SetValue(IsDisplayProperty, value); } } public static readonly DependencyProperty IsDisplayProperty = DependencyProperty.Register( "IsDisplay" , typeof ( bool ), typeof (TitlePasswordBox), new PropertyMetadata( false )); } |
2、控件模板
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | <Style TargetType= "controls:TitlePasswordBox" > <Setter Property= "BorderThickness" Value= "0" /> <Setter Property= "KeyboardNavigation.TabNavigation" Value= "None" /> <Setter Property= "HorizontalContentAlignment" Value= "Left" /> <Setter Property= "FocusVisualStyle" Value= "{x:Null}" /> <Setter Property= "Foreground" Value= "#30FFFFFF" /> <Setter Property= "AllowDrop" Value= "true" /> <Setter Property= "ScrollViewer.PanningMode" Value= "VerticalFirst" /> <Setter Property= "Stylus.IsFlicksEnabled" Value= "False" /> <Setter Property= "Foreground" Value= "White" ></Setter> <Setter Property= "SelectionBrush" Value= "#DDDDDD" ></Setter> <Setter Property= "CaretBrush" Value= "White" ></Setter> <Setter Property= "Template" > <Setter.Value> <ControlTemplate TargetType= "controls:TitlePasswordBox" > <Border x:Name= "border" BorderThickness= "{TemplateBinding BorderThickness}" BorderBrush= "#666666" SnapsToDevicePixels= "True" Background= "{Binding Background}" > <Grid> <TextBlock Text= "{TemplateBinding Title}" Foreground= "#858484" IsHitTestVisible= "False" VerticalAlignment= "Center" FontSize= "{Binding FontSize}" x:Name= "Title" Visibility= "Hidden" /> <PasswordBox Helper:PasswordBoxHelper.Attach= "True" Helper:PasswordBoxHelper.Password= "{Binding Path=Password,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" VerticalAlignment= "Center" Foreground= "#FFFFFF" CaretBrush= "White" BorderThickness= "0" Background= "Transparent" HorizontalContentAlignment= "Left" FontSize= "16" PasswordChar= "*" Margin= "-5,5,0,0" Visibility= "{TemplateBinding IsDisplay,Converter={StaticResource BoolToVisInverseConverter}}" /> <ScrollViewer x:Name= "PART_ContentHost" Focusable= "false" HorizontalScrollBarVisibility= "Hidden" VerticalScrollBarVisibility= "Hidden" Visibility= "{TemplateBinding IsDisplay,Converter={StaticResource BoolToVisConverter}}" /> </Grid> </Border> <ControlTemplate.Triggers> <Trigger Property= "IsMouseOver" Value= "true" > <Setter Property= "BorderBrush" TargetName= "border" Value= "#888888" /> </Trigger> <Trigger Property= "IsKeyboardFocused" Value= "true" > <Setter Property= "BorderBrush" TargetName= "border" Value= "#AAAAAA" /> </Trigger> <Trigger Property= "Text" Value= "{x:Null}" > <Setter Property= "Visibility" TargetName= "Title" Value= "Visible" /> </Trigger> <Trigger Property= "Text" Value= "" > <Setter Property= "Visibility" TargetName= "Title" Value= "Visible" /> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> <Style.Triggers> <MultiTrigger> <MultiTrigger.Conditions> <Condition Property= "IsInactiveSelectionHighlightEnabled" Value= "true" /> <Condition Property= "IsSelectionActive" Value= "false" /> </MultiTrigger.Conditions> <Setter Property= "SelectionBrush" Value= "#AAAAAA" /> </MultiTrigger> </Style.Triggers> </Style> |
3、界面按钮样式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <Border BorderBrush= "#30FFFFFF" BorderThickness= "1" > <StackPanel Orientation= "Horizontal" > <controls:TitlePasswordBox Margin= "5,0,0,0" Title= "{DynamicResource Login_BS0054}" HorizontalAlignment= "Left" IsDisplay= "{Binding ElementName=IsShow,Path=IsChecked}" x:Name= "Password" Text= "{Binding Password,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Width= "245" VerticalAlignment= "Center" BorderThickness= "0" Background= "Transparent" Foreground= "White" VerticalContentAlignment= "Center" /> <CheckBox x:Name= "IsShow" Grid.Row= "7" IsChecked= "False" Grid.Column= "2" HorizontalAlignment= "Left" Margin= "0,0,0,0" Width= "19.5" Height= "15" > <CheckBox.Style> <Style TargetType= "CheckBox" > <Setter Property= "FocusVisualStyle" Value= "{x:Null}" /> <Setter Property= "Template" > <Setter.Value> <ControlTemplate TargetType= "CheckBox" > <Image Source= "/Resource/Images/GameField/ConfigurationList/IDHidden.png" Stretch= "UniformToFill" x:Name= "img" /> <ControlTemplate.Triggers> <Trigger Property= "IsChecked" Value= "True" > <Setter Property= "Source" TargetName= "img" Value= "/Resource/Images/GameField/ConfigurationList/IDShow.png" /> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> </CheckBox.Style> </CheckBox> </StackPanel> </Border> |
4、代码中使用PasswordBoxHelper
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | public static class PasswordBoxHelper { public static readonly DependencyProperty PasswordProperty = DependencyProperty.RegisterAttached( "Password" , typeof ( string ), typeof (PasswordBoxHelper), new FrameworkPropertyMetadata( string .Empty, OnPasswordPropertyChanged)); public static readonly DependencyProperty AttachProperty = DependencyProperty.RegisterAttached( "Attach" , typeof ( bool ), typeof (PasswordBoxHelper), new PropertyMetadata( false , Attach)); private static readonly DependencyProperty IsUpdatingProperty = DependencyProperty.RegisterAttached( "IsUpdating" , typeof ( bool ), typeof (PasswordBoxHelper)); public static void SetAttach(DependencyObject dp, bool value) { dp.SetValue(AttachProperty, value); } public static bool GetAttach(DependencyObject dp) { return ( bool )dp.GetValue(AttachProperty); } public static string GetPassword(DependencyObject dp) { return ( string )dp.GetValue(PasswordProperty); } public static void SetPassword(DependencyObject dp, string value) { dp.SetValue(PasswordProperty, value); } private static bool GetIsUpdating(DependencyObject dp) { return ( bool )dp.GetValue(IsUpdatingProperty); } private static void SetIsUpdating(DependencyObject dp, bool value) { dp.SetValue(IsUpdatingProperty, value); } private static void OnPasswordPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) { PasswordBox passwordBox = sender as PasswordBox; passwordBox.PasswordChanged -= PasswordChanged; if (!( bool )GetIsUpdating(passwordBox)) { passwordBox.Password = ( string )e.NewValue; } passwordBox.PasswordChanged += PasswordChanged; } private static void Attach(DependencyObject sender, DependencyPropertyChangedEventArgs e) { PasswordBox passwordBox = sender as PasswordBox; if (passwordBox == null ) return ; if (( bool )e.OldValue) { passwordBox.PasswordChanged -= PasswordChanged; } if (( bool )e.NewValue) { passwordBox.PasswordChanged += PasswordChanged; } } private static void PasswordChanged( object sender, RoutedEventArgs e) { PasswordBox passwordBox = sender as PasswordBox; SetIsUpdating(passwordBox, true ); SetPassword(passwordBox, passwordBox.Password); SetIsUpdating(passwordBox, false ); } } |
PasswordBoxHelper编写,转载来源为:https://blog.csdn.net/cxb2011/article/details/89642096
以上代码就此完结,如有雷同请联系我
文章如有问题,欢迎指正
本文来自博客园,作者:会飞的飞机,转载请注明原文链接:https://www.cnblogs.com/FlyingPigs/p/17614442.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?