0

I am having trouble getting a custom ValidationRule to fire, when it is associated with an Expander.Header binding. In fact, the only place I can seem to get these custom rules to fire is in a DataGrid.RowValidationRules block...

The expander is defined in my Window XAML file like so;

<Expander Style="{StaticResource ValidatedSecondLevelExpanderStyle}">
    <Expander.Header>
        <Binding Path="Name" Mode="OneWay" ValidatesOnDataErrors="True" NotifyOnValidationError="True">
            <Binding.ValidationRules>
                <ValidationRules:BoundObjectIsValid />
            </Binding.ValidationRules>
        </Binding>
    </Expander.Header>
</Expander>

The bound property 'Name' is displayed correctly, but the validation rule 'BoundObjectIsValid' does not get invoked. Is this possible, and if so, what am I missing?

I know that I could alternately implement IDataErrorInfo on the bound object, however the object can't sensibly validate itself without some context that is provided by other parts of the system. Refactoring is possible, but I'd love to get the ValidationRules to work first!

RJ Lohan
  • 6,497
  • 3
  • 34
  • 54

1 Answers1

0

Refer to the msdn.

The binding engine checks each ValidationRule that is associated with a binding every time it transfers an input value, which is the binding target property value, to the binding source property.

So here in your case, you don't have an inpurt value being transfered to the source property since your Expander.header is not a control which you can use to input values.

Edit: But there is a property named ValidatesOnTargetUpdated' in the ValidationRule. When setting it to true, the validationrule will be applied when the target property is updated

Colin
  • 551
  • 2
  • 10
  • I've gotten the DataGrid to validate rows when they are displayed - not changed by the user. I assume there's a way to do this with any Binding, since it's an available setting...? – RJ Lohan Dec 06 '12 at 00:35
  • 1
    @RJLohan You can try to set the property 'ValidatesOnTargetUpdated' of ValidationRule to true. I just find out there is such a property. Hoping this helps. – Colin Dec 06 '12 at 01:23
  • Looks like this property makes a difference, not sure why though, as it's not applied in the other case. – RJ Lohan Dec 06 '12 at 02:40