wpfbindingshorthandvalidationrules

Add ValidationRules into a single xaml line OR shorthand ValidationRules


I'm using a PasswordBox which exposes a dependency property such that I can bind to it. The problem is that by using it like so, I cannot shorthand the Binding.ValidationRules to this syntax:

<PasswordBox services:RPLPasswordBoxBinder.BindPassword="True" 
             services:RPLPasswordBoxBinder.BoundPassword="{Binding Path=LoginUser.Parola, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"> 
</PasswordBox>

I set my ValidationRules to a textbox like this:

<TextBox.Text>
   <Binding Path="LoginUser.Parola" UpdateSourceTrigger="PropertyChanged">
       <Binding.ValidationRules>
           <some validation rule/>
        </Binding.ValidationRules>
   </Binding>
</TextBox.Text>

Is there any way to specify the ValidationRules collection to my PasswordBox in a single xaml line? Or maybe there's another clever solution for validating user input into my password box?

Some clarifications:

I'm using MVVM and I don't want to use code behind.

I want to add only a single ValidationRule. Maybe the problem with shorthanding Binding.ValidationRules is that this property is a collection. One validationrule would suffice in my situation.

There's a similar question on stackoverflow here. My problem is different as I don't just want to increase readability but actually validate my PasswordBox.


Solution

  • I suggest that you base your data model class on IDataErrorInfo and then validation is performed there and not in the code behind.

    There are plenty of examples, but here's one for starters and another here.