问题描述
人们stackoverflow.我正在使用MVVM,我已经 ViewModel 用属性密码调用UserViewModel.在视图中,有一个密码框控件.
Hi people stackoverflow. I'm working with MVVM, I have ViewModel call UserViewModel with a Property Password. In the View have a control PasswordBox.
<PasswordBox x:Name="txtPassword" Password="{Binding Password}" />
但是这个xaml不起作用.您如何进行绑定?请帮忙!!
But this xaml don't work. How do you do the binding?? Help please!!
推荐答案
出于安全原因,Password属性不是依赖项属性,因此您无法绑定到该属性.不幸的是,您需要在老式方法后面的代码中执行绑定(注册OnPropertyChanged事件并通过代码更新值...)
For security reasons the Password property is not a dependency property and therefore you can't bind to it. Unfortunately you'll need to perform the binding in the code behind the old fashioned way (register for OnPropertyChanged event and update the value through code...)
我的快速搜索将我带到了此博客文章展示了如何编写附加属性来避开该问题.这样做是否值得,实际上取决于您是否偏爱代码隐藏.
I quick search brings me to this blog post which shows how to write an attached property to sidestep the issue. Whether this is worth doing or not though really depends on your aversion to code-behind.
这篇关于带有MVVM的PasswordBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!