群星之主WPF支持绑定的密码框( 二 )


群星之主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支持绑定的密码框
文章图片
摘要
在这篇短文中 , 我们已经了解了如何自定义一个支持密码绑定的密码框 , 虽然说的是不安全 , 不过 , 谁在乎呢 。 祝各位好运 , 编码愉快 , 摆脱996 , 回归955!


推荐阅读