Custom ValidationRule - data binding property
- Is it possible to have a custom validationrule where the property is data bound?
Let say I want the MinimumLength data bound to a value from a textbox instead of being hard coded.
<TextBox Width="200" Margin="5">
<TextBox.Text>
<Binding Path="Address" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<local
tringRangeValidationRule
MinimumLength="{Binding Path=Text, ElementName=txt1, Mode=Default}"</Binding.ValidationRules>
MaximumLength="30"
ErrorMessage="Address is required and must be less than 30 letters." />
</Binding>
</TextBox.Text>
</TextBox>
Answers
If this is for .NET 3.5, have a look at the new IDataErrorInfo support. The binding validation story in 3.0 is much weaker.

ValidationRule is not a dependency object (nor is it in the element tree), so you cannot set bindings on dependency properties on it (nor would they be resolved if you could).
If txt1 appears higher in the tree, you could use my ObjectReference markup extension to supply a reference to the TextBox as a property on the validation rule:
Code Block<TextBox Name="txt1"
dw:ObjectReference.Declaration="{dw:ObjectReference theTextBox}">
. . .
<
TextBox Width="200" Margin="5"><
TextBox.Text><
Binding Path="Address" UpdateSourceTrigger="PropertyChanged"><
Binding.ValidationRules><
local: StringRangeValidationRule TextBox="{dw:ObjectReference theTextBox}" MaximumLength="30" ErrorMessage="Address is required and must be less than 30 letters." /></
Binding.ValidationRules></
Binding></
TextBox.Text></
TextBox>This, of course, assumes that you add a TextBox property to the validation rule and then modify the rule to look at the Text property of the supplied control (or some similar [more dynamic] approach). Not an ideal solution, but such things are necessary in 3.0 when it comes to validation.
If txt1 is lower in the tree, then you can use the ObjectReference extension to supply a reference to the root framework element of the window/page and add a second property to your validation rule that indicates the name of the object you care about ("txt1"). Then you can use rootElement.FindName("txt1") in the validation rule.
All Replies
If this is for .NET 3.5, have a look at the new IDataErrorInfo support. The binding validation story in 3.0 is much weaker.

ValidationRule is not a dependency object (nor is it in the element tree), so you cannot set bindings on dependency properties on it (nor would they be resolved if you could).
If txt1 appears higher in the tree, you could use my ObjectReference markup extension to supply a reference to the TextBox as a property on the validation rule:
Code Block<TextBox Name="txt1"
dw:ObjectReference.Declaration="{dw:ObjectReference theTextBox}">
. . .
<
TextBox Width="200" Margin="5"><
TextBox.Text><
Binding Path="Address" UpdateSourceTrigger="PropertyChanged"><
Binding.ValidationRules><
local: StringRangeValidationRule TextBox="{dw:ObjectReference theTextBox}" MaximumLength="30" ErrorMessage="Address is required and must be less than 30 letters." /></
Binding.ValidationRules></
Binding></
TextBox.Text></
TextBox>This, of course, assumes that you add a TextBox property to the validation rule and then modify the rule to look at the Text property of the supplied control (or some similar [more dynamic] approach). Not an ideal solution, but such things are necessary in 3.0 when it comes to validation.
If txt1 is lower in the tree, then you can use the ObjectReference extension to supply a reference to the root framework element of the window/page and add a second property to your validation rule that indicates the name of the object you care about ("txt1"). Then you can use rootElement.FindName("txt1") in the validation rule.


