0

If I have the following binding

<TextBox Text="{Binding XXX.Name, ValidatesOnNotifyDataErrors=True}"/>

it doesn't work because only the DataContext implements INotifyDataErrorInfo and raises "XXX.Name" errors but ValidatesOnNotifyDataErrors tries to monitor XXX for error events not the data context.

However I am sure somebody could figure out how to write an attached property to do the following

<TextBox Grid.Column="5" Text="{Binding Binding.Name, c:ValidatesOnNotifyDataErrorsOnDataContext=True}"/>

where the data context is monitored not the child. Anybody got an idea how to start with that?

Community
  • 1
  • 1
bradgonesurfing
  • 30,949
  • 17
  • 114
  • 217

1 Answers1

1

I think this is possible to implement, but because of the flexibility of bindings (RelativeSource, MultiBindings and whatnot) it would be difficult to make something like this that is truly robust. Personally, I think it would be cleaner to to implement INotifyDataErrorInfo at every level of the structure (and for parts of the structure that you don't own, like your Point example, use proxy classes that mirror the properties).

Anyway, Binding is a MarkupExtension, not a DependencyObject, which means attached properties can't be applied to it. You could inherit Binding to add your own properties, but this isn't very useful since it doesn't give you any overridable methods.

It shouldn't be necessary to extend Binding though, since all you want is a custom ValidationRule. Setting ValidatesOnNotifyDataErrors=True is equivalent to adding a NotifyDataErrorValidationRule:

<TextBox>
    <TextBox.Text>
        <Binding Path="XXX.Name">
            <Binding.ValidationRules>
                <NotifyDataErrorValidationRule />
            </Binding.ValidationRules>
        </Binding>
    </TextBox>
</TextBox>

So you just need to replace NotifyDataErrorValidationRule with your own rule. If you override this Validate overload (which is passed the binding expression), you should be able to access the full binding path (through ParentBinding) and look up an error.

nmclean
  • 7,564
  • 2
  • 28
  • 37