Orionlk
Orionlk

Reputation: 278

Disable ValidatesOnDataErrors if IsEnabled property is False WPF

I have a program in MVVM pattern and I have a View which contains some objects, one of them is a Value TextBox which ValidatesOnDataErrors when writing a text in it.

Also I have a Binding to its IsEnabled property which changed on the ViewModel by some conditions I made.

I want be able to eliminate ValidatesOnDataErrors (make it False) when the binding propery of IsEnabled is False, How I can do that?

<StackPanel>
   .....

   <TextBox Text="{Binding Path=Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged,ValidatesOnDataErrors=True}" IsEnabled="{Binding isValueFieldEnable}"/>

   .....

</StackPanel>

Thank you.

Upvotes: 0

Views: 487

Answers (1)

mm8
mm8

Reputation: 169160

As @Il Vic suggests this validation logic should be implemented in the view model class.

You don't change the ValidatesOnDataErrors property of the binding in the view. Instead, you should make sure that your view model is always in a valid state and return no errors for the data-bound Value property whenever the isValueFieldEnable property returns false.

The binding in the view is always the same. It is your view model that is supposed to define whether the Value property is set to a valid value. And the property should Always considered to be valid when the isValueFieldEnable is false. That's your validation logic. And this should be implemented in your view model class, not in your view.

Upvotes: 1

Related Questions