WPF: Confirmed Verification Password

I have 2 PasswordBoxes. I need to check if passwords are equal. I do not want to write this condition in the [] .xaml.cs code, but I want to mark the password in red if the passwords are not equal.

Should I write a special ValidationRule, some code in a ViewModel, or something else? Can someone help me? Now the check is performed in the [] .xaml.cs file, but I want to avoid this.

+3
source share
2 answers

Using:

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

Code (this code does not apply to all cases):

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];
    }
   };
  }
 }

In addition, you can define a dependency property with an error style and set it instead of BorderBrush. But I do not know how to use the standard ErrorTemplate in this case.

+2
source

readonly Model PasswordConfirmBox,

, ,

 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}"/>
0

All Articles