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、具体示例代码

    样式代码

<Style TargetType="PasswordBox" x:Key="mypasswd">
	<Setter Property="Height" Value="30"/>
	<Setter Property="Width" Value="200"/>
	<Setter Property="FontSize" Value="14"/>
	<Setter Property="Padding" Value="2 0 0 0"/>
	<Setter Property="PasswordChar" Value="*"/>
	<Setter Property="BorderThickness" Value="1"/>
	<Setter Property="Background" Value="#555a64"/>
	<Setter Property="Foreground" Value="#dddddd"/>
	<Setter Property="BorderBrush" Value="#bbbbbb"/>
	<Setter Property="CaretBrush" Value="#dddddd"/>
	<Setter Property="HorizontalContentAlignment" Value="Left"/>
	<Setter Property="VerticalContentAlignment" Value="Center"/>
	<Setter Property="KeyboardNavigation.TabNavigation" Value="None"/>
	<Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst"/>
	<Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
	<Setter Property="VerticalAlignment" Value="Center"/>
	<Setter Property="HorizontalAlignment" Value="Left"/>
	<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
	<Setter Property="AllowDrop" Value="true"/>
	<Setter Property="MinHeight" Value="30"/>
	<Setter Property="Template">
		<Setter.Value>
			<ControlTemplate TargetType="PasswordBox">
				<Border x:Name="bd" Height="{TemplateBinding Height}" Width="{TemplateBinding Width}" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" CornerRadius="3" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}">
					<ScrollViewer x:Name="PART_ContentHost" VerticalAlignment="Center" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden"/>
				</Border>
				<ControlTemplate.Triggers>
					<Trigger Property="IsMouseOver" Value="true">
						<Setter Property="BorderBrush" TargetName="bd" Value="#2561a9"/>
					</Trigger>
					<Trigger Property="IsFocused" Value="true">
						<Setter Property="BorderBrush" Value="#2561a9" TargetName="bd"/>
					</Trigger>
					<Trigger Property="IsEnabled" Value="false">
						<Setter Property="Opacity" TargetName="bd" Value="0.5"/>
					</Trigger>
				</ControlTemplate.Triggers>
			</ControlTemplate>
		</Setter.Value>
	</Setter>
</Style>

    使用控件

<WrapPanel Margin="10">
	<!--BorderThickness默认为1、VerticalContentAlignment默认为Top-->
	<PasswordBox Width="150" Height="35" Password="1234546" PasswordChar="*" FontSize="15" Foreground="#dddddd" Style="{x:Null}"
				 Background="#555a64" BorderBrush="YellowGreen" BorderThickness="1" Margin="10" MaxLength="11"/>
	
	<PasswordBox Width="150" Height="35" Password="1234546"  FontSize="15" Foreground="#dddddd" VerticalContentAlignment="Center"
				 Background="Transparent" BorderBrush="YellowGreen" BorderThickness="0 0 0 1" Margin="10" MaxLength="11" HorizontalContentAlignment="Center"
				 />
	
	<PasswordBox Width="150" Height="35" Password="1234546"  FontSize="15" Foreground="#dddddd" VerticalContentAlignment="Center"
				 Background="Transparent" BorderBrush="YellowGreen" BorderThickness="0 0 0 1" Margin="10" MaxLength="11" HorizontalContentAlignment="Center"
				 Style="{StaticResource mypasswd}"/>

	<PasswordBox Width="150" Height="35" Password="1234546" FontSize="15" Foreground="#dddddd"  Style="{x:Null}" VerticalContentAlignment="Center"
				 Background="#555a64" BorderBrush="YellowGreen" BorderThickness="1" Margin="10" MaxLength="11"/>
	
	<PasswordBox Width="150" Height="35" Password="1234546" PasswordChar="#" FontSize="15" VerticalContentAlignment="Center" Foreground="#dddddd"
				 Background="#555a64" BorderBrush="YellowGreen" BorderThickness="0" Margin="10" PasswordChanged="PasswordBox_PasswordChanged"
				  Style="{StaticResource mypasswd}"/>	
</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控件样式和模板,实现水印密码框。

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

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值