1

I am working with MVVM and my stage is: I have two PasswordBox

        <PasswordBox x:Name="Password" Width="150" Height="30" />
        <PasswordBox x:Name="PasswordConfirmation" Width="150" Height="30" />

I want do a validation into xaml between Password and PasswordConfirmation. Similar as would do JQuery in website.

I don't want break MVVM, I mean, I don't want to have code behind.

CampDev
  • 1,659
  • 3
  • 16
  • 22

1 Answers1

0

If you can tolerate the code for an IMultiValueConverter, the following is very MVVM.

<Page.Resources>
    <local:ConfirmPasskey x:Key="ConfirmPasskey" />
</Page.Resources>

...

<PasswordBox x:Name="_PassKey" Grid.Column="1" Grid.Row="1" Margin="0,5,5,0"
    LostFocus="Update_PassCheckError"/>
<PasswordBox x:Name="_PassCheck" Grid.Column="1" Grid.Row="2" Margin="0,5,5,0" 
    LostFocus="Update_PassCheckError" />
<TextBlock x:Name="_PassCheckError" Grid.ColumnSpan="2" Grid.Row="3" Margin="5,5,5,5"
    Foreground="Red" HorizontalAlignment="Center" >
        <TextBlock.Text>
            <MultiBinding Converter="{StaticResource ConfirmPasskey}" Mode="OneWay" >
                <Binding ElementName="_PassKey" />
                <Binding ElementName="_PassCheck" />
            </MultiBinding>
         </TextBlock.Text>
 </TextBlock>

I use a TextBlock because I want to provied a pseudo adornment, but any dependent property could be used,... such as a boolean for the IsEnabled property of a button of a login prompt. Here is my IMultiValueConverter, which could return a bool instead of the password validation text:

public class ConfirmPasskey : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parm, System.Globalization.CultureInfo culture)
    {
        bool match = (values[0] as PasswordBox).Password.Equals((values[1] as PasswordBox).Password);

        if (targetType.Name.Equals("Nullable`1"))
            return match;

        return (match) ? null : "Passcodes do not match.";
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parm, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
ergohack
  • 1,268
  • 15
  • 27