gpt4 book ai didi

c# - 密码框不会屏蔽我的密码 WPF

转载 作者:行者123 更新时间:2023-12-02 17:41:27 24 4
gpt4 key购买 nike

我在 WPF 中使用密码框,在编辑模式下,密码框不会屏蔽我的密码。

这是我的代码示例:

<PasswordBox Width="200" Password="{Binding Path=Password, ValidatesOnDataErrors=True, NotifyOnValidationError=True, Mode=TwoWay}"  HorizontalAlignment="Left"/>

我尝试包含 Passwordchar = '*'

最佳答案

出于安全原因,您不能直接在 View 模型中获取密码值,您必须创建依赖属性。

下面是密码代码。

开发者:

public static readonly DependencyProperty BoundPassword =
DependencyProperty.RegisterAttached("BoundPassword", typeof(string), typeof(PasswordBoxHelper), new PropertyMetadata(string.Empty, OnBoundPasswordChanged));

public static readonly DependencyProperty BindPassword = DependencyProperty.RegisterAttached(
"BindPassword", typeof(bool), typeof(PasswordBoxHelper), new PropertyMetadata(false, OnBindPasswordChanged));

private static readonly DependencyProperty UpdatingPassword =
DependencyProperty.RegisterAttached("UpdatingPassword", typeof(bool), typeof(PasswordBoxHelper), new PropertyMetadata(false));

private static void OnBoundPasswordChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var box = d as PasswordBox;
if(d == null || !GetBindPassword(d))
{
return;
}
box.PasswordChanged -= HandlePasswordChanged;

var newPassword = (string)e.NewValue;

if(!GetUpdatingPassword(box))
{
box.Password = newPassword;
}

box.PasswordChanged += HandlePasswordChanged;
}

private static void OnBindPasswordChanged(DependencyObject dp, DependencyPropertyChangedEventArgs e)
{

var box = dp as PasswordBox;

if(box == null)
{
return;
}

bool wasBound = (bool)(e.OldValue);
bool needToBind = (bool)(e.NewValue);

if(wasBound)
{
box.PasswordChanged -= HandlePasswordChanged;
}

if(needToBind)
{
box.PasswordChanged += HandlePasswordChanged;
}
}

private static void HandlePasswordChanged(object sender, RoutedEventArgs e)
{
var box = sender as PasswordBox;
SetUpdatingPassword(box, true);
SetBoundPassword(box, box.Password);
SetUpdatingPassword(box, false);
}

public static void SetBindPassword(DependencyObject dp, bool value)
{
dp.SetValue(BindPassword, value);
}

public static bool GetBindPassword(DependencyObject dp)
{
return (bool)dp.GetValue(BindPassword);
}

public static string GetBoundPassword(DependencyObject dp)
{
return (string)dp.GetValue(BoundPassword);
}

public static void SetBoundPassword(DependencyObject dp, string value)
{
dp.SetValue(BoundPassword, value);
}

private static bool GetUpdatingPassword(DependencyObject dp)
{
return (bool)dp.GetValue(UpdatingPassword);
}

private static void SetUpdatingPassword(DependencyObject dp, bool value)
{
dp.SetValue(UpdatingPassword, value);
}

下面是 XAML:

  <PasswordBox x:Name="PasswordBox" 
utility:PasswordBoxHelper.BindPassword="true"
utility:PasswordBoxHelper.BoundPassword="{Binding Path=Password,
Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

关于c# - 密码框不会屏蔽我的密码 WPF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20186737/

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