I have a WPF application with a UI that contains a checkbox and a textbox. The checkbox and textbox are bound to properties on my business object. I've been using validation rules to validate user input and for the most part, they're pretty straight forward (checking that the value is not null/empty, checking that a value is within a certain range, etc). FWIW, here's my existing XAML:
<StackPanel>
<CheckBox x:Name="requirePinNumberCheckBox" IsChecked="{Binding Path=RequirePinNumber, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">Require PIN number</CheckBox>
<TextBox x:Name="pinNumberTextBox" Style="{StaticResource textBoxInError}" PreviewTextInput="pinNumberTextBox_PreviewTextInput">
<TextBox.Text>
<Binding Path="PinNumber" Mode="TwoWay" UpdateSourceTrigger="LostFocus">
<Binding.ValidationRules>
<local:PinNumberValidationRule ValidationStep="RawProposedValue"/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
</StackPanel>
My validation rule for the textbox is simply:
public class PinNumberValidationRule : ValidationRule
{
public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
{
// make sure that the PIN number isn't blank
if (string.IsNullOrEmpty(value.ToString()))
{
return new ValidationResult(false, "PIN number cannot be blank.");
}
else
{
return new ValidationResult(true, null);
}
}
}
Unlike most of my other validation scenarios, the ValidationRule for the textbox should only apply if the checkbox is checked (or rather when the boolean property that the checkbox is bound to is set to TRUE). Can anyone tell me how to implement something like this? Thanks!