Pass WPF UserControl reference to another UserControl
- by Rob Bell
I've created two UserControls, a ValidationManager and a ValidationOutput. On a given form there is one ValidationManager and several ValidationOutput controls, one for each control that is validated. The ValidationManager is given a list of validation errors when the form is submitted, I want each ValidationOutput control to look at this list and see if there are any errors relevant to them.
The code looks a bit like this:
<r:ValidationManager x:Name="myValidationManager" />
...
<TextBox Name="SomeField" />
<r:ValidationOutput FieldName="SomeField" />
I need to pass a reference to the ValidationManager to each of the ValidationOutput controls. I've added a ValidationManager property to the ValidationOutput UserControl but don't know how to pass the reference to the control. I've tried the following but am just clutching at straws:
<r:ValidationOutput ValidationManager="myValidationManager" />
...and...
<r:ValidationOutput ValidationManager="{Binding myValidationManager}" />
The first results in an error "Property 'ValidationManager' was not found or is not serializable for type 'ValidationOutput'" and the second "A 'Binding' cannot be set on the 'ValidationManager' property of type 'ValidationControl'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject."