【WPF】自定义PassWordBox (可以绑定的) 、SecureString类型
目的
自定义一个可以绑定的密码输入框
知识点:自定义控件、 SecureString类型
System.Security.SecureString(表示应保密的文本) 保存非托管内存中,需要用指针逐个字符的读取。
正常的String类型值,在脱离开作用域之后,其值在内存中并不会被立即销毁,这时如果有人恶意扫描你的内存,程序中所保存的机密信息就会暴露;于是就有了System.Security.SecureString,SecureString表示一个应保密的文本,它在初始化时就已被加密,并且脱离作用域后会被立即销毁;
自定义用户控件
1、添加自定义用户控件类
2、xaml代码
<UserControl x:Class="Login.Theme.CustomeControls.PasswordBoxBingable" 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:Login.Theme.CustomeControls" mc:Ignorable="d" d:DesignHeight="40" d:DesignWidth="300"> <!--DesignHeight和DesignWidth属性,它们在设计时控制用户控件的大小(在运行时,用户控件的大小将由容纳它的容器决定)--> <!--自定义控件中不支持 子定控件的style设置,要定义自定义控件样式只能在 使用控件的地方定义--> <Grid> <PasswordBox x:Name="Psb" BorderBrush="White" VerticalAlignment="Stretch" VerticalContentAlignment="Center" HorizontalAlignment="Stretch" HorizontalContentAlignment="Left" Padding="10,0,0,0" BorderThickness="0" Background="Transparent" > </PasswordBox> </Grid> </UserControl>
注意: d:DesignHeight="40" d:DesignWidth="300", DesignHeight和DesignWidth属性,它们在设计时控制用户控件的大小(在运行时,用户控件的大小将由容纳它的容器决定)
C#代码
using System; using System.Collections.Generic; using System.Linq; using System.Security; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace Login.Resources.CustomeControls { /// <summary> /// BingablePassword.xaml 的交互逻辑 /// </summary> /// public partial class PasswordBoxBingable : UserControl { public static readonly DependencyProperty PasswordProperty = DependencyProperty.Register("Password", typeof(SecureString), typeof(PasswordBoxBingable)); public SecureString Password { get { return (SecureString)GetValue(PasswordProperty); } set { SetValue(PasswordProperty, value); } } public PasswordBoxBingable() { InitializeComponent(); Psb.PasswordChanged += OnPasswordChanged; } private void OnPasswordChanged(object sender, RoutedEventArgs e) { Password = Psb.SecurePassword; } } }
显示
应用自定义控件
登入窗体 LoginView.xaml
1)在主窗体中 引入自定义控件PasswordBoxBingable所在的命名空间
<Window x:Class="Login.Views.LoginView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:CustomControl="clr-namespace:Login.Resources.CustomeControls"
2)应用自定义控件PasswordBoxBingable,并且绑定 LoginViewModel.cs的password。
<CustomControl:PasswordBoxBingable Style="{DynamicResource PasswordBoxBingableStyle2}" x:Name="psp2" Password="{Binding Password,Mode=OneWayToSource,UpdateSourceTrigger=PropertyChanged}" Height="40" Margin="20,20,20,0"/>
3)给自定义控件设置样式 PasswordBoxBingable,样式单独写入到PasswordBingableStyle.xaml 文件中。
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:CustomeControls="clr-namespace:Login.Resources.CustomeControls"> <Style x:Key="PasswordBoxBingableStyle2" TargetType="{x:Type CustomeControls:PasswordBoxBingable}"> <Setter Property="BorderThickness" Value="1"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type CustomeControls:PasswordBoxBingable}"> <Border x:Name="boder" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true"> <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/> </Border> <ControlTemplate.Triggers> <Trigger Property="IsEnabled" Value="False"> <Setter Property="Opacity" Value="0.56"/> </Trigger> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="BorderBrush" Value="#7AAEE3"/> </Trigger> <Trigger Property="IsMouseOver" Value="False"> <Setter Property="BorderBrush" Value="#A4A7AD"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> </ResourceDictionary>
4)登入窗体 LoginView.xaml.cs
public partial class LoginView: Window { public LoginView() { InitializeComponent(); this.DataContext = new LoginViewModel(); }
}
5)部分LoginViewModel.cs 代码如下
public class LoginViewModel : BaseViewModel { public SecureString Password { get; set; } public string UserName{ get; set; } }
最终效果如下:
本文只为了介绍自定用户控件和SecureString类型的应用。
编程是个人爱好