如何在MVVM中将PasswordBox与wpf绑定 [英] How to bind PasswordBox with wpf in MVVM
本文介绍了如何在MVVM中将PasswordBox与wpf绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请告诉我如何在MVVM中将PasswordBox与wpf绑定
Please tell me how to bind the PasswordBox with wpf in MVVM
推荐答案
出于安全原因,WPF没有提供任何Dependency属性在密码框中。如果你真的想要实现你想要的,那么你可以关注 Samual Jack's [ ^ ]博客文章,他提供了一个优雅的解决方案。
For security reason WPF didn't gave any Dependency property in Password box. How ever if you really want to achieve what you want then you can follow Samual Jack's[^] blog post where he gave an elegant solution.
问题是使用PasswordBox没有Idependency属性:S
The problem is that with PasswordBox there is no Idependency property :S
使用下面的附加属性绑定密码框
Use the below attached property to bind password box
<pre>public class BoundPasswordBox
{
public static readonly DependencyProperty PasswordProperty = DependencyProperty.RegisterAttached("Password", typeof(string), typeof(BoundPasswordBox),
new FrameworkPropertyMetadata(string.Empty, OnPasswordPropertyChanged));
public static readonly DependencyProperty AttachProperty =DependencyProperty.RegisterAttached("Attach",typeof(bool), typeof(BoundPasswordBox),
new PropertyMetadata(false, Attach));
private static readonly DependencyProperty IsUpdatingProperty =DependencyProperty.RegisterAttached("IsUpdating", typeof(bool),
typeof(BoundPasswordBox));
public static void SetAttach(DependencyObject dp, bool value)
{
dp.SetValue(AttachProperty, value);
}
public static bool GetAttach(DependencyObject dp)
{
return (bool)dp.GetValue(AttachProperty);
}
public static string GetPassword(DependencyObject dp)
{
return (string)dp.GetValue(PasswordProperty);
}
public static void SetPassword(DependencyObject dp, string value)
{
dp.SetValue(PasswordProperty, value);
}
private static bool GetIsUpdating(DependencyObject dp)
{
return (bool)dp.GetValue(IsUpdatingProperty);
}
private static void SetIsUpdating(DependencyObject dp, bool value)
{
dp.SetValue(IsUpdatingProperty, value);
}
private static void OnPasswordPropertyChanged(DependencyObject sender,DependencyPropertyChangedEventArgs e)
{
PasswordBox passwordBox = sender as PasswordBox;
passwordBox.PasswordChanged -= PasswordChanged;
if (!(bool)GetIsUpdating(passwordBox))
{
passwordBox.Password = (string)e.NewValue;
}
passwordBox.PasswordChanged += PasswordChanged;
}
private static void Attach(DependencyObject sender,DependencyPropertyChangedEventArgs e)
{
PasswordBox passwordBox = sender as PasswordBox;
if (passwordBox == null)
return;
if ((bool)e.OldValue)
{
passwordBox.PasswordChanged -= PasswordChanged;
}
if ((bool)e.NewValue)
{
passwordBox.PasswordChanged += PasswordChanged;
}
}
private static void PasswordChanged(object sender, RoutedEventArgs e)
{
PasswordBox passwordBox = sender as PasswordBox;
SetIsUpdating(passwordBox, true);
SetPassword(passwordBox, passwordBox.Password);
SetIsUpdating(passwordBox, false);
}
}
添加附加属性的命名空间(here util:)并使用如下所示:
Add the namespace of attached property ("here util:") and use like below:
<passwordbox x:name="TxtPassword" util:boundpasswordbox.attach="True" util:boundpasswordbox.password="{Binding Path=Password,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged,ValidatesOnDataErrors=True,NotifyOnValidationError=True}" xmlns:util="#unknown" xmlns:x="#unknown">
</passwordbox>
这篇关于如何在MVVM中将PasswordBox与wpf绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文