سؤال

Introduction

I have two TextBox in my view, each binded to some properties in my view-model (Property1, Property2).

TextBox are alternatively enabled upon some boolean, and properties, are validated using IDataErrorInfo in the view-model + some styling in the view.

Problem

I would like to disable validation style when items are disabled.

NB1: Currently, solution I found is to change validation scheme directly in the view-model, but this requires to notify for property changes in order to force the view to re-read IDataErrorInfo (while properties haven't really changed, only selector ...)

NB2: My problem is really close to this one, but the description and the solutions are far too complex for me to really get the point.

Pseudo-Code

<UserControl 

    <UserControl.Resources>
        <Style TargetType="{x:Type Control}" x:Key="ControlValidationStyle">
            ...
        </Style> 
    </UserControl.Resources>

     ...

    <TextBox  
             Text="{Binding Property1, 
                            ValidatesOnDataErrors=True, 
                            UpdateSourceTrigger=PropertyChanged}" 

             IsEnabled="{Binding IsMode1}"

             Style="{StaticResource ControlValidationStyle}"
     />

    <TextBox  
             Text="{Binding Property2, 
                            ValidatesOnDataErrors=True, 
                            UpdateSourceTrigger=PropertyChanged}" 

             IsEnabled="{Binding IsMode1, 
                                 Converter={StaticResource BoolInverse}}"

             Style="{StaticResource ControlValidationStyle}"
     />

</UserControl>

ControlValidationStyle

<Style TargetType="{x:Type Control}" x:Key="ControlValidationStyle">
    <Style.Resources>
        <Style TargetType="ToolTip">
            <Setter Property="Background" Value="Tomato" />
            <Setter Property="BorderBrush" Value="Red" />
            <Setter Property="BorderThickness" Value="2" />
            <Setter Property="Foreground" Value="white" />
        </Style>
    </Style.Resources>
    <Style.Triggers>
        <Trigger Property="Validation.HasError" Value="true">
            <Setter Property="ToolTip" 
                        Value="{Binding RelativeSource={RelativeSource Self},
                        Path=(Validation.Errors)[0].ErrorContent}" />
            <Setter Property="Background" Value="Bisque"/>
            <Setter Property="BorderBrush" Value="Red" />
            <Setter Property="BorderThickness" Value="2" />
            <Setter Property="Foreground" Value="Red" />
        </Trigger>
    </Style.Triggers>
</Style>
هل كانت مفيدة؟

المحلول

Why wont you use MultiTrigger instead of Trigger:

<MultiTrigger>
    <MultiTrigger.Conditions>
      <Condition Property="Validation.HasError" Value="true" />
      <Condition Property="IsEnabled" Value="true"  />
    </MultiTrigger.Conditions>
 <Setter .../>
</MultiTrigger>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top