I'm currently using the validation code listed here in an application. I'd like to selectively validate a page. What I mean by that is this: I have a form that's databound with validation rules attached. I also have a checkbox that, when checked, disables and clears the values of several bound textboxes. Is there a property I can set on these textboxes to tell the validation engine not to include these when validating?
A:
Possibly, you could have triggers that would switch the bindings. Meaning the Setter-s would rebind the property using another bindings with different validation rules.
But that would only(?) work if all those validation rules are in a ControlTemplate or DataTemplate.
arconaut
2009-05-05 21:04:40
+1
A:
Perhaps if you wrote an attached property for your Validator class, and checked for it first thing in your IsValid method.
static class Validator
{
public static readonly DependencyProperty SkipValidationProperty =
DependencyProperty.RegisterAttached("SkipValidation", typeof(bool), typeof(Validator),
new UIPropertyMetadata(false));
public static bool GetSkipValidation(DependencyObject obj)
{
return (bool)obj.GetValue(SkipValidationProperty);
}
public static void SetSkipValidation(DependencyObject obj, bool value)
{
obj.SetValue(SkipValidationProperty, value);
}
public static bool IsValid(DependencyObject parent)
{
if (Validator.GetSkipValidation(parent)) return true;
//Rest of the validation code
}
}
Then you could do something as simple as:
<StackPanel>
<TextBox x:Name="txt" local:Validator.SkipValidation="{Binding IsChecked, ElementName=cbx}" />
<CheckBox x:Name="cbx" >Skip Validate?</CheckBox>
</StackPanel>
statenjason
2009-05-06 23:14:12