gpt4 book ai didi

c# - 如何清除通过行为绑定(bind)到 ViewModel 的 WPF PasswordBox?

转载 作者:行者123 更新时间:2023-11-30 13:54:29 27 4
gpt4 key购买 nike

我编写 WPF MVVM Prism 6.2 应用程序。在登录窗口(即 PrismUserControl)的 View 中,我有一个密码框绑定(bind)(通过行为)到 View 模型中的“密码”属性。 每次在应用程序运行时调用登录窗口时,PasswordBox 必须为空。 (例如,在用户关闭当前 session 后,他或她必须只能看到空的 Shell 和 Shell 上方的登录窗口.) 我的问题是上述 PasswordBox 在应用程序加载后仅第一次显示为空。如果密码框在第二次或第三次等时显示,则它不为空。请看下图:

enter image description here

如您所见,密码不为空,但在本例中必须为空。下面是来自登录窗口标记的 XAML 片段,其中密码框是:

. . . . . . . . . . . . . .
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
. . . . . . . . . . . . . .
<PasswordBox Grid.Row="1" Grid.Column="1" Height="30" Margin="0 10 5 0" AutomationProperties.AutomationId="UserPasswordBox">
<i:Interaction.Behaviors>
<behavior:PasswordBoxBindingBehavior Password="{Binding Password}"/>
</i:Interaction.Behaviors>
</PasswordBox>
. . . . . . . . . . . . . . . .

如上所示,下面是 XAML 中也涉及的行为类:

public class PasswordBoxBindingBehavior : Behavior<PasswordBox>
{
protected override void OnAttached()
{
AssociatedObject.PasswordChanged += OnPasswordBoxValueChanged;
}

public SecureString Password
{
get { return (SecureString)GetValue(PasswordProperty); }
set { SetValue(PasswordProperty, value); }
}

public static readonly DependencyProperty PasswordProperty =
DependencyProperty.Register("Password", typeof(SecureString), typeof(PasswordBoxBindingBehavior), new PropertyMetadata(null));

private void OnPasswordBoxValueChanged(object sender, RoutedEventArgs e)
{
var binding = BindingOperations.GetBindingExpression(this, PasswordProperty);
if (binding != null)
{
PropertyInfo property = binding.DataItem.GetType().GetProperty(binding.ParentBinding.Path.Path);
if (property != null)
property.SetValue(binding.DataItem, AssociatedObject.SecurePassword, null);
}
}
}

下面是 View 模型中的“密码”属性。 PasswordBox 通过 PasswordBoxBindingBehavior 绑定(bind)到此属性:

public SecureString Password
{
get { return this._password; }
set { this.SetProperty(ref this._password, value); }
}

在应用程序工作期间每次显示登录窗口时,我都需要 PasswordBox 为空。我试图以编程方式清除 View 模型中的“密码”属性,但没有帮助。我该怎么做?请帮忙。

最佳答案

您可以为设置 PasswordBox< 的 Password 属性的行为的 Password 依赖属性连接一个 PropertyChangedCallback当 View 模型的 Password 源属性设置为 null 时, 为空字符串:

public class PasswordBoxBindingBehavior : Behavior<PasswordBox>
{
protected override void OnAttached()
{
AssociatedObject.PasswordChanged += OnPasswordBoxValueChanged;
}

public SecureString Password
{
get { return (SecureString)GetValue(PasswordProperty); }
set { SetValue(PasswordProperty, value); }
}

public static readonly DependencyProperty PasswordProperty =
DependencyProperty.Register("Password", typeof(SecureString), typeof(PasswordBoxBindingBehavior), new PropertyMetadata(OnSourcePropertyChanged));

private static void OnSourcePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if(e.NewValue == null)
{
PasswordBoxBindingBehavior behavior = d as PasswordBoxBindingBehavior;
behavior.AssociatedObject.PasswordChanged -= OnPasswordBoxValueChanged;
behavior.AssociatedObject.Password = string.Empty;
behavior.AssociatedObject.PasswordChanged += OnPasswordBoxValueChanged;
}
}

private static void OnPasswordBoxValueChanged(object sender, RoutedEventArgs e)
{
PasswordBox passwordBox = sender as PasswordBox;
var behavior = Interaction.GetBehaviors(passwordBox).OfType<PasswordBoxBindingBehavior>().FirstOrDefault();
if(behavior != null)
{
var binding = BindingOperations.GetBindingExpression(behavior, PasswordProperty);
if (binding != null)
{
PropertyInfo property = binding.DataItem.GetType().GetProperty(binding.ParentBinding.Path.Path);
if (property != null)
property.SetValue(binding.DataItem, passwordBox.SecurePassword, null);
}
}
}
}

然后您可以通过简单地将 View 模型中的 Password 源属性设置为 null 来清除 PasswordBox

关于c# - 如何清除通过行为绑定(bind)到 ViewModel 的 WPF PasswordBox?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42315356/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com