WPF支持绑定的密码框

副标题:让WPF密码框支持绑定
介绍
喜欢WPFMVVM数据绑定的人 , 都知道 , 绑定功能是一个及其强大 , 并让人开心的功能 。 但是好多新手 , 在做一个99%的系统都需要登录功能时 , 居然遇到了一个非常奇葩的问题 。 密码框居然不能绑定 。 对 , 不能绑定!!!这就尴尬了 , 不是说要前后台分离么 , 为啥这个地方要给我当头一棒 , 啊啊啊啊啊啊啊啊 。
必应了一波 , 找到了一个大概的原因 。 好像是因为微软为了安全 , 特意没有为PwsswordBox的Password添加依赖属性 , 听说是因为绑定的话 , 密码是明文的 。 然而这有什么用么 , 反正最终你还是要通过网络把密码传出去 , 在那个地方不一样可以获取到密码嘛 。 所以啊 , 我们干脆点 , 你没有依赖属性 , 我给你加一个不就好了 。 哈哈哈哈哈哈哈!!!!
具体代码
WPF支持绑定的密码框
文章图片
usingSystem.Windows;
usingSystem.Windows.Controls;
namespaceWPF自定义密码绑定框
{
///<summary>
///密码绑定的帮助类
///</summary>
publicclassBindPasswordBox
{
privatestaticbool_updating=false;
///<summary>
///BindPassword依赖属性
///</summary>
publicstaticreadonlyDependencyPropertyBindPasswordProperty=
DependencyProperty.RegisterAttached(''BindPassword'',
typeof(string),
typeof(BindPasswordBox),
newFrameworkPropertyMetadata(string.Empty,OnBindPasswordChanged));
///<summary>
///获取密码
///</summary>
publicstaticstringGetBoundPassword(DependencyObjectdependencyObject)
{
return(string)dependencyObject.GetValue(BindPasswordProperty);
}
///<summary>
///设置密码
///</summary>
publicstaticvoidSetBindPassword(DependencyObjectdependencyObject,stringvalue)
{
dependencyObject.SetValue(BindPasswordProperty,value);
}
///<summary>
///密码改变事件
///</summary>
privatestaticvoidOnBindPasswordChanged(
DependencyObjectd,
DependencyPropertyChangedEventArgse)
{
PasswordBoxpassword=dasPasswordBox;
if(password!=null)
{
//禁用密码改变事件
password.PasswordChanged-=PasswordChanged;
}
if(e.NewValue!=null)
{
if(!_updating)
{
password.Password=e.NewValue.ToString();
}
}
else
{
password.Password=string.Empty;
}
//启用密码改变事件
password.PasswordChanged+=newRoutedEventHandler(PasswordChanged);
}
///<summary>
///密码改变事件
///</summary>
staticvoidPasswordChanged(objectsender,RoutedEventArgse)
{
PasswordBoxpassword=senderasPasswordBox;
if(password!=null)
{
_updating=true;
SetBindPassword(password,password.Password);
_updating=false;
}
}
}
}
示例程序
使用它也很简单 , 只要在XAML添加xmlns:local=''clr-namespace:WPF自定义密码绑定框''就好了 。 对了 , 我用的MvvmLight,所以还需要在nuget中引用 。 另外 , 不要忘记添加DataContext=''{BindingSource={StaticResourceLocator},Path=Main}''哦 , 不然的话 , 有的小伙伴就不会用了 。
WPF支持绑定的密码框
文章图片
<Windowx:Class=''WPF自定义密码绑定框.MainWindow''
xmlns=''http://schemas.microsoft.com/winfx/2006/xaml/presentation''
xmlns:d=''http://schemas.microsoft.com/expression/blend/2008''
xmlns:x=''http://schemas.microsoft.com/winfx/2006/xaml''
xmlns:mc=''http://schemas.openxmlformats.org/markup-compatibility/2006''
xmlns:local=''clr-namespace:WPF自定义密码绑定框''
DataContext=''{BindingSource={StaticResourceLocator},Path=Main}''
mc:Ignorable=''d''
Title=''MainWindow''Height=''200''Width=''300''>
<Grid>
<Grid.RowDefinitions>
<RowDefinitionHeight=''Auto''/>
<RowDefinition/>
</Grid.RowDefinitions>
<StackPanelOrientation=''Horizontal''>
<ButtonContent=''获取密码''Margin=''5''Width=''120''Height=''30''Command=''{BindingGetPasswordCommand}''/>
</StackPanel>
<PasswordBoxWidth=''200''Height=''30''Grid.Row=''1''VerticalContentAlignment=''Center''
local:BindPasswordBox.BindPassword=''{BindingPath=LoginPassword,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}''
/>
</Grid>
</Window>
usingCommonServiceLocator;
usingGalaSoft.MvvmLight.Ioc;
namespaceWPF自定义密码绑定框.ViewModel
{
publicclassViewModelLocator
{
publicViewModelLocator()
{
ServiceLocator.SetLocatorProvider(()=>SimpleIoc.Default);
SimpleIoc.Default.Register<MainViewModel>();
}
publicMainViewModelMain
{
get
{
returnServiceLocator.Current.GetInstance<MainViewModel>();
}
}
publicstaticvoidCleanup()
{
}
}
}
usingGalaSoft.MvvmLight;
usingGalaSoft.MvvmLight.Command;
usingSystem;
usingSystem.Windows;
namespaceWPF自定义密码绑定框.ViewModel
{
publicclassMainViewModel:ViewModelBase
{
publicMainViewModel()
{
GetPasswordCommand=newRelayCommand(GetPassword);
}
#region获取密码
///<summary>
///获取密码
///</summary>
privatevoidGetPassword()
{
MessageBox.Show(LoginPassword);
}
#endregion
#region登录密码
privatestring_LoginPassword;
///<summary>
///登录密码
///</summary>
publicstringLoginPassword{get{return_LoginPassword;}set{_LoginPassword=value;RaisePropertyChanged();}}
#endregion
#region命令
publicRelayCommandGetPasswordCommand{get;set;}
#endregion
}
}
重点
记得添加这两句话 , 不然不会生效 。
xmlns:local=''clr-namespace:WPF自定义密码绑定框''
DataContext=''{BindingSource={StaticResourceLocator},Path=Main}''
WPF支持绑定的密码框
文章图片
摘要
【WPF支持绑定的密码框】在这篇短文中 , 我们已经了解了如何自定义一个支持密码绑定的密码框 , 虽然说的是不安全 , 不过 , 谁在乎呢 。 祝各位好运 , 编码愉快 , 摆脱996 , 回归955!


    推荐阅读