• WPF 控件专题 PasswordBox控件详解


    1、PasswordBox 介绍

        PasswordBox 密码输入控件;

        PasswordBox 具有对浮泡 MouseUp 和 MouseDown 事件的内置处理。 因此,永远不会调用侦MouseUp听或从中PasswordBox侦听事件的MouseDown自定义事件处理程序。 如果需要响应这些事件,请改为侦听隧道 PreviewMouseUp 和 PreviewMouseDown 事件,或将处理程序注册到 HandledEventsToo 参数 (后一个选项只能通过代码) 使用。 请勿标记所处理的事件,除非有意禁用 PasswordBox 对这些事件的本机处理,并且请注意,这对控件的 UI 有显著影响。

        PasswordBox看起来与TextBox类似,但它通过显示圆圈符号字符串来屏蔽实际字符(可通过设置PasswordChar属性选择不同的屏蔽字符)。此外,PassworBox控件不支持剪切板,从而不能复制内部的文本。 

    **************************************************************************************************************

    2、常用属性介绍

        FontFamily:字体系列;    FontSize:字体大小;    FontStretch:字体在屏幕上紧缩或加宽的程度;FontWeight:字体粗细;
        
        Background:背景;    BorderBrush:边框颜色;    BorderThickness:边框宽度;    Foreground:前景色;
        
        Width/Height:宽度/高度;    Name:元素标识名称;    IsEnabled:使能,是否可用;    Margin:外边距;
        
        Opacity:透明度;    Visibility:可见性;    IsVisible:是否可见;    FlowDirection:其子元素的流动方向;
        
        LayoutTransform:在执行布局时应该应用于此元素的图形转换方式。    RenderTransform:元素的呈现位置的转换信息;
        
        RenderTransformOrigin:由RenderTransform声明的任何可能呈现转换的中心点,相对于元素的边界。
        
        HorizontalAlignment/VerticalAlignment:在父元素中组合此元素时所应用的水平对齐特征/垂直对齐特征。
        
        HorizontalContentAlignment/VerticalContentAlignment:控件内容的水平对齐方式/垂直对齐方式。

        Password:获取或设置 PasswordBox 当前保留的密码。

        PasswordChar:获取或设置 PasswordBox 的掩码字符。

    **************************************************************************************************************

    3、具体示例代码

        样式代码

    1. <Style TargetType="PasswordBox" x:Key="mypasswd">
    2. <Setter Property="Height" Value="30"/>
    3. <Setter Property="Width" Value="200"/>
    4. <Setter Property="FontSize" Value="14"/>
    5. <Setter Property="Padding" Value="2 0 0 0"/>
    6. <Setter Property="PasswordChar" Value="*"/>
    7. <Setter Property="BorderThickness" Value="1"/>
    8. <Setter Property="Background" Value="#555a64"/>
    9. <Setter Property="Foreground" Value="#dddddd"/>
    10. <Setter Property="BorderBrush" Value="#bbbbbb"/>
    11. <Setter Property="CaretBrush" Value="#dddddd"/>
    12. <Setter Property="HorizontalContentAlignment" Value="Left"/>
    13. <Setter Property="VerticalContentAlignment" Value="Center"/>
    14. <Setter Property="KeyboardNavigation.TabNavigation" Value="None"/>
    15. <Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst"/>
    16. <Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
    17. <Setter Property="VerticalAlignment" Value="Center"/>
    18. <Setter Property="HorizontalAlignment" Value="Left"/>
    19. <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
    20. <Setter Property="AllowDrop" Value="true"/>
    21. <Setter Property="MinHeight" Value="30"/>
    22. <Setter Property="Template">
    23. <Setter.Value>
    24. <ControlTemplate TargetType="PasswordBox">
    25. <Border x:Name="bd" Height="{TemplateBinding Height}" Width="{TemplateBinding Width}" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" CornerRadius="3" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}">
    26. <ScrollViewer x:Name="PART_ContentHost" VerticalAlignment="Center" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden"/>
    27. Border>
    28. <ControlTemplate.Triggers>
    29. <Trigger Property="IsMouseOver" Value="true">
    30. <Setter Property="BorderBrush" TargetName="bd" Value="#2561a9"/>
    31. Trigger>
    32. <Trigger Property="IsFocused" Value="true">
    33. <Setter Property="BorderBrush" Value="#2561a9" TargetName="bd"/>
    34. Trigger>
    35. <Trigger Property="IsEnabled" Value="false">
    36. <Setter Property="Opacity" TargetName="bd" Value="0.5"/>
    37. Trigger>
    38. ControlTemplate.Triggers>
    39. ControlTemplate>
    40. Setter.Value>
    41. Setter>
    42. Style>

        使用控件

    1. <WrapPanel Margin="10">
    2. <PasswordBox Width="150" Height="35" Password="1234546" PasswordChar="*" FontSize="15" Foreground="#dddddd" Style="{x:Null}"
    3. Background="#555a64" BorderBrush="YellowGreen" BorderThickness="1" Margin="10" MaxLength="11"/>
    4. <PasswordBox Width="150" Height="35" Password="1234546" FontSize="15" Foreground="#dddddd" VerticalContentAlignment="Center"
    5. Background="Transparent" BorderBrush="YellowGreen" BorderThickness="0 0 0 1" Margin="10" MaxLength="11" HorizontalContentAlignment="Center"
    6. />
    7. <PasswordBox Width="150" Height="35" Password="1234546" FontSize="15" Foreground="#dddddd" VerticalContentAlignment="Center"
    8. Background="Transparent" BorderBrush="YellowGreen" BorderThickness="0 0 0 1" Margin="10" MaxLength="11" HorizontalContentAlignment="Center"
    9. Style="{StaticResource mypasswd}"/>
    10. <PasswordBox Width="150" Height="35" Password="1234546" FontSize="15" Foreground="#dddddd" Style="{x:Null}" VerticalContentAlignment="Center"
    11. Background="#555a64" BorderBrush="YellowGreen" BorderThickness="1" Margin="10" MaxLength="11"/>
    12. <PasswordBox Width="150" Height="35" Password="1234546" PasswordChar="#" FontSize="15" VerticalContentAlignment="Center" Foreground="#dddddd"
    13. Background="#555a64" BorderBrush="YellowGreen" BorderThickness="0" Margin="10" PasswordChanged="PasswordBox_PasswordChanged"
    14. Style="{StaticResource mypasswd}"/>
    15. WrapPanel>

    **************************************************************************************************************

    4、效果图

    **************************************************************************************************************

    5、总结和扩展

      与TextBox类相比,PassworBox的用户界面更精简。与TextBox类非常相似,它提供了MaxLength属性;Clear()、Paste()以及SelectAll()方法;并且提供了当文本发生变化时触发的事件(PasswordChanged事件)。TextBox类和PassworBox类最重要的区别在于内部的工作方式。尽管可使用Password属性作为普通字符串读取和设置文本,但在内部PasswordBox类只使用System.Security.SecureString对象。

        与普遍文本非常类似,SecureString是纯文本对象。区别是在内存中的存储方式。SecureString以加密方式在内存中保存。用于加密字符串的密钥是随机生成的,存储在一块从来不会写入到磁盘的内存中。最终的结果即使计算机崩溃,恶意用户也不可能通过检查页面文件来检索密码数据。即使找到,也只能找到加密版本。

        SecureString类还提供了根据需要丢弃内容的功能。当调用SecureString.Dispose()方法时,内存中的密码数据就会被改写。这样可保证所有密码信息从内存中被改写擦除,并且不能再以任何方式使用。正如所期望的,当控件被销毁时,PassworBox控件会自动为保存在内存中的SecureString对象调用Dispose()方法。

        我们也可以修改PasswordBox控件样式和模板,实现水印密码框。

    **************************************************************************************************************

  • 相关阅读:
    TD Target Algorithms
    什么是React属性钻取(Prop Drilling)
    共享购模式:数据驱动的消费增值新体验
    探究-ping指令的使用
    MACBOOK M1芯片上安装mongdb遇到的问题,以及安装教程
    React富文本编辑器开发(十一)命令与编辑器
    一文看懂推荐系统:排序10:wide&deep模型,wide就是LR负责记忆,deep负责高阶特征交叉而泛化
    docker学习笔记
    pytorch 优化器
    Java 异步和同步有什么不同呢?
  • 原文地址:https://blog.csdn.net/BYH371256/article/details/125495565