Integrating with WPF

Microsoft Enterprise Library 5.0

DropDown image DropDownHover image Collapse image Expand image CollapseAll image ExpandAll image Copy image CopyHover image

You can integrate the Validation Application Block with Windows Presentation Foundation (WPF) applications. For example, you can use the application block to validate the values in UI controls in accordance with rules defined for bound properties of an object. Validation is performed as part of the general Binding mechanism. For information about the WPF binding mechanism, see Data Binding Overview on MSDN. The validation rules you specify can be configured to validate at different stages in the binding processing pipeline. For information about applying validation rules, see How to: Implement Binding Validation and ValidationStep Enumeration on MSDN.


Note:
The Validation Application Block cannot be used in XML Browser Applications (XBAP) due to issues with the partial trust environment that XBAP mandates.


To use the WPF integration feature, you must add a reference to the assembly Microsoft.Practices.EnterpriseLibrary.Validation.Integration.WPF. The integration is performed through a custom ValidationRule named ValidatorRule defined in this assembly, which performs its validation by invoking the Validation Application Block validator for a specific property on a given type. Validation only occurs if a binding exists, and that binding is connected (if the source of the binding exists).

You can add a validation rule directly to a binding by specifying the desired type and property names, as shown here.

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

You can also set the RulesetName and ValidationSpecificationSource properties to refine how the validator for the specified property is created.

If the value of the validated control that carries the Required validation attribute is empty to begin with, and remains empty during validation, the source is not updated and validation does not occur. In this particular case the ValidateOnTargetUpdate property will not work either because the null default value of the target will not change. Instead, you can invoke UpdateSource on the binding to force validation to occur, as shown here.

C# Copy Code
this.Required.GetBindingExpression(TextBox.TextProperty).UpdateSource();
Visual Basic Copy Code
Me.Required.GetBindingExpression(TextBox.TextProperty).UpdateSource()

The validation rule operates in the ConvertedProposedValue step, after the value has been converted but before it is set on the source. This means that value conversion errors must be detected through other mechanism. One approach is to enable the ValidatesOnDataErrors property on the validated binding.

Configuring Validation through Attached Properties

Setting up ValidatorRules directly requires you to duplicate information that is already specified in the binding. For example, in the previous declaration of a TextBox, the ValidatorRule source property name duplicates the validated binding's path. An alternative mechanism to set up a ValidatorRule by using a custom attached property. The following shows an example.

XAML Copy Code
<TextBox x:Name="TextBox1" 
         Text="{Binding ValidatedStringProperty, UpdateSourceTrigger=PropertyChanged}" 
         vab:Validate.BindingForProperty="Text" />

Setting the Validate.BindingForProperty to the name of a bound property in the control adds a ValidatorRule for the specified property (in the example above, this is the Text property of the TextBox). This rule will validate the value using the validator associated with the source property in the binding.

To specify the rule set and the validation specification source, you can use the two attached properties Validate.UsingRulesetName and Validate.UsingSource. This avoids the possibility of an error if the dependency property does not have a binding, or the binding path is not a simple property name.

The attached properties for a binding are configured to be available for all FrameworkElement subclasses, allowing tools such as Microsoft Blend to show these properties in the standard property grid when a control is selected.