开发者

WPF: validation confirmed password

开发者 https://www.devze.com 2023-03-04 20:19 出处:网络
I have 2 PasswordBoxes. I need to che开发者_如何学JAVAck are passwords equal. I don\'t want to write this condition into [].xaml.cs code, but i want to mark PasswordBox in red when passwords aren\'t e

I have 2 PasswordBoxes. I need to che开发者_如何学JAVAck are passwords equal. I don't want to write this condition into [].xaml.cs code, but i want to mark PasswordBox in red when passwords aren't equal.

Should i write special ValidationRule, some code in ViewModel or something else? Can anyone help me? Now the validation is written in the [].xaml.cs but i want to avoid it.


Using:

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

Code (this code is not cover 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];
    }
   };
  }
 }

Also, it's possible to define dependency property with style of error and setting it instead of BorderBrush. But i don't know how to use in this case the standard ErrorTemplate.


Create a readonly property in your viewModel and return a brush for the border of the PasswordConfirmBox and then bind the textbox to the property

EDIT after comments received I've not tested this and there might be minor errors but this way you can return a different type depending on the converterparameter

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

Value Converters:

    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

精彩评论

暂无评论...
验证码 换一张
取 消