Validate child property ViewModel with WPF integration

Topics: Validation Application Block
Nov 22, 2010 at 10:15 PM

I would like to validate my objects hosted to ViewModel from wpf view with WPF Integration.
I have a ViewModel with domain objects directly exposed so I have to validate a child property of viewmodel.
A scenario like this:
<TextBox Margin="2,5,14,5" Name="textBoxName" Text="{Binding Path=Person.Name}" vab:Validate.BindingForProperty="Text"   Grid.Column="1" Grid.Row="0" VerticalContentAlignment="Center"/>

Is it possible? Actually i receive error using Validate.BindingForProperty the problem is because i am using child property.


Nov 23, 2010 at 1:42 AM

You need to make use of the SourceType attribute.  Specify the type of the child property, in your case, I assume it is of type Person.

<TextBox x:Name="TextBox1">
  <TextBox.Text>
    <Binding Path="Name" UpdateSourceTrigger="PropertyChanged">
      <Binding.ValidationRules>
        <vab:ValidatorRule 
             SourceType="{x:Type test:Person}" 
             SourcePropertyName="ValidatedStringProperty"/>
      </Binding.ValidationRules>
    </Binding>
  </TextBox.Text>
</TextBox>

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Nov 24, 2010 at 8:23 AM

Thankyou for the information. I would like to find a more productiv way(with less code to write). Maybe i could implement a custom validator rule? Did you think it is possible and right?

About this what's the main difference between the standard wpf validator and the "vab" validator? Maybe ...the first one need an IDataErrorInfo whereas "vab" work directly with binded endity validatin it with vab validator?

 

Thankyou

Nov 25, 2010 at 3:39 AM

I can't find a way to make it work without using a separate element for vab:ValidatorRule and specifying the SourceType attribute. 

I do think it's possible to create your own custom validator rule but if it were up to me, I'd rather use an existing one which works rather than creating a new implementation.

By the way, the one I posted above is incorrect, supposing your parent object is of type Foo and it has a property named Person1 of type Person.  The correct configuration would be:

<TextBox x:Name="TextBox1">
  <TextBox.Text>
    <Binding Path="Person1" UpdateSourceTrigger="PropertyChanged">
      <Binding.ValidationRules>
        <vab:ValidatorRule 
             SourceType="{x:Type test:Person}" 
             SourcePropertyName="Name"/>
      </Binding.ValidationRules>
    </Binding>
  </TextBox.Text>
</TextBox>

On the topic of the difference between the standardx wpf and entlib's wpf validation, I haven't really used the WPF built in validation but from what I have read, WPF has only one built in validation rule which is the ExceptionValidationRule.  If you want other validation logic, you would have to implement the ValidationRule class.  With entlib, it comes with a lot of built-in validators which you can use and specify either in configuration or through attributes.

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com