0

I want to make WPF validations run only if the control that is being validated is Visible.
In my project, depending on the user's choice, some controls are Collapsed.

I have a button that is enabled only if there are no validation errors (I use WPF MultiValue Converter and check if all controls are valid - HasError is false for all controls).

Validations are implemented using ValidationRules.

So, my goal is to validate all currently Visible controls (Only Visible!), instead of Button being disabled because collapsed fields are empty...
Is there a way to make Validation Error raise only when control is Visible and input is Invalid?

I have searched on the internet for a few days, but I haven't found any solutions for this situation...

Thanks in advance! Best regards!

Vasilije Bursac
  • 185
  • 1
  • 2
  • 15

2 Answers2

1

If you use xaml for setting your rules the code below do what yuo want. Trigger for visibility, no binding - no validation) I used TextBox only as example. Also you can use other approach, pass to your ValidationRule your property which shows that elements are hidden, and check it inside rules How to bind values from xaml to validation rule?

<TextBox Visibility="{Binding IsVisible, Converter={wpfApp1:BoolToVisibilityConverter}}">
        <TextBox.Text>
            <Binding Path="Text" UpdateSourceTrigger="PropertyChanged">
                <Binding.ValidationRules>
                    <wpfApp1:TextValidationRule/>
                </Binding.ValidationRules>
            </Binding>
        </TextBox.Text>
        <TextBox.Style>
            <Style TargetType="{x:Type TextBox}">
                <Style.Triggers>
                    <Trigger Property="Visibility" Value="Collapsed">
                        <Setter Property="Text" Value="{x:Null}"/>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </TextBox.Style>
    </TextBox>
Lana
  • 1,024
  • 1
  • 7
  • 14
  • Thank you very much for your answer! I have tried this method, but the control still has `Validation.HasError` set to `True` even if it is Collapsed, so my button is still disabled. Do you know how can I affect this, too? I thought that removing validation as you did, will reset the `Validation.HasError` to `False`, but unfortunately, that is not the case... – Vasilije Bursac Jan 29 '21 at 12:34
1

You can add property to your validation rule like Enabled and use it while validating (return successfull validation if not enabled). Then bind that property in XAML to either Visibility property of the control or whatever determines if control is visible (ie. CheckBox's IsChecked property). Edit: Code for clarity:

<CheckBox Name="chkName" />
<TextBox Visibility="{Binding IsChecked, ElementName=chkName, Convertor...}">
    <TextBox.Text>
        <Binding Path="ModelProperty">
            <Binding.ValidationRules>
                <wpfApp1:TextValidationRule Enabled="{Binding IsChecked, ElementName=chkName}" />
            </Binding.ValidationRules>
        </Binding>
    </TextBox.Text>
</TextBox>
Shadowed
  • 956
  • 7
  • 19
  • 1
    you can't directly bind property to ValidationRule – Lana Jan 16 '21 at 19:57
  • @Shadowed Thank you very much for your answer! Unfortunately, as Lana said, you can't directly bind a property to ValidationRule, since ValidationRule does not inherit from DependencyObject... This answer uses the same idea, but gets more in details on how you can overcome this: https://stackoverflow.com/questions/46519802/how-to-bind-values-from-xaml-to-validation-rule – Vasilije Bursac Feb 11 '21 at 15:50