您的位置:首页 > 其它

如何绑定PasswordBox控制中的Password属性

2015-11-08 18:19 459 查看
网上有同学分享了通过中间Buffer绑定Passwordbox的password属性,感觉有点麻烦。发现一简单方法拿出来和大家分享。。


步骤1:

在引用 中增加 :

System.Windows.Interactivity 

步骤2:

在xaml中增加:

xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"


<PasswordBox x:Name="passwordBox" HorizontalAlignment="Left" Height="43" Margin="365,10,0,0" VerticalAlignment="Top" Width="374">
<i:Interaction.Triggers>
<span style="white-space:pre">	</span><i:EventTrigger EventName="PasswordChanged">
<span style="white-space:pre">	</span><i:InvokeCommandAction Command="{Binding PasswordChanged}" CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type PasswordBox}}}"/>
<span style="white-space:pre">	</span></i:EventTrigger>
</i:Interaction.Triggers>
</PasswordBox>


在ViewModel中增加:

private string pwd;
public string PWD
{
get { return pwd; }
set
{
pwd = value;
this.RaisePropertyChanged("pwd");
}
}


public ICommand PasswordChanged
{
get
{
return new DelegateCommand<PasswordBox>((pb) =>
{
if(pb != null)
{
PWD = pb.Password;
}
});
}
}


OK了,试试看吧!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: