带有 MVVM 的密码框 [英] PasswordBox with MVVM

查看:17
本文介绍了带有 MVVM 的密码框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,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!!

推荐答案

出于安全原因,密码属性不是依赖属性,因此您无法绑定到它.不幸的是,您需要在老式方法背后的代码中执行绑定(注册 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 的密码框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆