views:

129

answers:

1

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."

+1  A: 

It should be possible to use a StaticResource to assign the ValidationManager to your properties:

<r:ValidationOutput ValidationManager="{StaticResource myValidationManager}" />

However, the design seems strange to me. Maybe you should better use MVVM as Rich pointed out in his comment.

gehho
Using StaticResource gives me the error "Property 'ValidationManager' was not found or is not serializable for type 'ValidationOutput'". Any ideas?
Rob Bell
Re the above, this is how I'm exposing the property (apologies for formatting): public ValidationManager ValidationManager { set { _validationManager = value; _validationManager.ValidationComplete += ValidationManager_ValidationComplete; } }
Rob Bell
Hmmm, this seems to be a designer problem. The code does compile and run, doesn't it?! I do not know the reason for this error, so maybe you would have to google for that, or start another question here.
gehho