views:

415

answers:

1

Hy,

I have a validator set on the text property of a textblock. For a correct validation I would need the parent usercontrol of the textblock, but the only things I have in the validator are the value object (a string) and the culture (doesn't help either).

Does anyone know a way to get certain usercontrols in a class/a method where I have no access to any kind of visual or control of my application.

The problem could be solve if I could give the validator the usercontrol or the textblock as parameters, but I didn't find a way to do so..

Thank you very much for your answers.

Greets

A: 

You could try validating using two validation properties built into the Binding class: ValidatesOnExceptions and ValidatesOnDataErrors.

With ValidatesOnExceptions, the binding is invalid if an exception is thrown during the conversion process. In this case, you could create a custom converter, pass the user control as a parameter, and then throw an exception in the converter if the binding was invalid.

<Binding ValidatesOnExceptions="True"/>

With ValidatesOnDataErrors, the binding is invalid if the source implements IDataErrorInfo and returns a non-null/empty string for the IDataErrorInfo.Error property. If your source does or could implement this interface, you could validate the data on the source side.

<Binding ValidatesOnDataErrors="True"/>
Josh G
The problem is, that in the validator I haven't enough data (which would be provided by the usercontrol) to validate the data correctly. So these two validations don't help me figuring out if the data is correct or not. In the IDataErrorInfo I also don't get more data than the string of the textblock, which unfortunately is not enough to validate my data.
dalind
Can't you create a custom converter and pass the converter the user control that you need? Converters are allowed to receive arguments in the binding process. It appears that validators are not.
Josh G
Ideally I would suggest creating a wrapper for your model (a ViewModel) that contained the necessary properties and services to perform the validation on its own (and then validates binding through IDataErrorInfo).
Josh G