WPF:验证确认的密码

本文关键字:密码 确认 验证 WPF | 更新日期: 2023-09-27 17:56:09

我有2个密码框。我需要检查密码是否相等。我不想将此条件写入 [].xaml.cs 代码中,但我想在密码不相等时将 PasswordBox 标记为红色。

我应该编写特殊的验证规则,ViewModel中的一些代码还是其他东西?谁能帮我?现在验证写在 [].xaml 中.cs但我想避免它。

WPF:验证确认的密码

使用:

<PasswordBox Name="tbPassword" />
<PasswordBox Name="tbPasswordConf" />
<PasswordValidator 
      Box1="{Binding ElementName=tbPassword}" 
      Box2="{Binding ElementName=tbPasswordConf}" />
代码

(此代码并不涵盖所有情况):

public class PasswordValidator : FrameworkElement
 {
  static IDictionary<PasswordBox, Brush> _passwordBoxes = new Dictionary<PasswordBox, Brush>();
  public static readonly DependencyProperty Box1Property = DependencyProperty.Register("Box1", typeof(PasswordBox), typeof(PasswordValidator), new PropertyMetadata(Box1Changed));
  public static readonly DependencyProperty Box2Property = DependencyProperty.Register("Box2", typeof(PasswordBox), typeof(PasswordValidator), new PropertyMetadata(Box2Changed));
  public PasswordBox Box1
  {
   get { return (PasswordBox)GetValue(Box1Property); }
   set { SetValue(Box1Property, value); }
  }
  public PasswordBox Box2
  {
   get { return (PasswordBox)GetValue(Box2Property); }
   set { SetValue(Box2Property, value); }
  }
  private static void Box1Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
  {
  }
  private static void Box2Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
  {
   var pv = (PasswordValidator)d;
   _passwordBoxes[pv.Box2] = pv.Box2.BorderBrush;
   pv.Box2.LostFocus += (obj, evt) =>
   {
    if (pv.Box1.Password != pv.Box2.Password)
    {
     pv.Box2.BorderBrush = new SolidColorBrush(Colors.Red);
    }
    else
    {
     pv.Box2.BorderBrush = _passwordBoxes[pv.Box2];
    }
   };
  }
 }

此外,可以使用错误样式定义依赖项属性,并设置它而不是 BorderBrush。但是我不知道在这种情况下如何使用标准错误模板。

在视图模型中创建一个只读属性,并为密码确认框的边框返回画笔,然后将文本框绑定到该属性

收到评论后编辑 我没有测试过这个,可能会有小错误,但这样你可以根据转换器参数返回不同的类型

 public bool PasswordValidation
    {
        get
        {
            if (textBox1.Text == textBox2.Text)
                return true;
            else
                return
                    false;
        }
    }

值转换器:

    public class ValConverter : IValueConverter
{
    #region IValueConverter Members
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (parameter == "")
        {
            if ((value as bool?) ?? false)
                return Visibility.Hidden;
            else
                return Visibility.Visible;
        }
        else if (parameter == "")
        {
            if ((value as bool?) ?? false)
                return new SolidColorBrush(Colors.Black);
            else
                return new SolidColorBrush(Colors.Red);
        }
    }
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (parameter == "Visibility")
        {
            if ((System.Windows.Visibility)value == Visibility.Visible)
                return false;
            else
                return true;
        }
        else if (parameter == "Brush")
        {
            if (((SolidColorBrush)value).Color == Colors.Black)
                return true;
            else
                return false;
        }
    }

Xaml:

<Window.Resources>
    <vis:ValConverter x:Key="valConverter"/>
</Window.Resources>
<TextBox Name="textBox1"/>
<TextBox Name="textBox2" BorderBrush="{Binding ConfirmBorder,Converter={StaticResource valConverter},ConverterParameter=Brush}" />
<TextBlock Text="Passwords Does not Match!" BorderBrush="{Binding ConfirmBorder,Converter={StaticResource valConverter},ConverterParameter=Visibility}"/>