带有 MVVM 的密码盒
大家好,stackoverflow。 我正在使用 MVVM,我有 ViewModel 使用属性密码调用 UserViewModel。 在View中有一个控件PasswordBox。
<PasswordBox x:Name="txtPassword" Password="{Binding Password}" />
但是这个xaml不起作用。 请问怎么绑定啊?? 请帮助!!
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}" />
But this xaml don't work. How do you do the binding?? Help please!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
出于安全原因,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.
您始终可以编写一个包装密码并为密码属性添加依赖属性的控件。
我只会使用后面的代码,但如果你必须的话,你可以这样做:
You can always write a control that wraps the Password and adds a dependency property for the Password property.
I would just use code behind, but if you must you can do something like:
BindablePasswordBox 有问题。 它仅在一个方向上起作用,即PasswordBox 到PasswordProperty。 下面是它的修改版本,可以双向工作。 它注册一个PropertyChangedCallback并在调用时更新PasswordBox的密码。
我希望有人觉得这很有用。
There is an issue with the BindablePasswordBox. It only works in one direction, PasswordBox to PasswordProperty. Below is a modified version of it that works in both directions. It registers a PropertyChangedCallback and updates the PasswordBox's Password when it is called.
I hope that someone finds this useful.
为了避免密码在任何时候以纯文本形式存在于内存中,我将该值作为参数提供给我的命令。
然后在我的视图模型中。
虽然直接从绑定提供 SecurePassword 是有意义的,但它似乎总是提供空值。 所以这不起作用:
To avoid having the password available in memory as plain text at any point, I provide the value as a parameter to my command.
Then in my view model.
While it would make sense to provide the SecurePassword directly from the binding, it always seems to provide an empty value. So this does NOT work:
检查密码框上的另一个线程。
最好不要将密码保存在任何 DP 或公共财产上。
密码框上的其他线程
Check another thread on password box.
Its better not to keep the password on any DP or public property.
Other thread on passwordbox