启用或禁用上下文验证。

3

介绍

在我的视图中,我有两个TextBox,每个都绑定到我的视图模型中的一些属性(Property1, Property2)。

根据布尔值和属性,在视图中启用交替的TextBox,并使用IDataErrorInfo在视图模型中验证这些属性以及视图中的某些样式。

问题

当项目被禁用时,我想禁用验证样式。

NB1:目前,我发现的解决方案是直接更改视图模型中的验证方案,但这需要通知属性更改,以便强制视图重新读取IDataErrorInfo(虽然属性没有真正变化,只是选择器)。

NB2:我的问题与这个问题非常相似,但描述和解决方案对我来说过于复杂,很难理解。

伪代码

<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>

控件验证样式

<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>
1个回答

6
为什么不使用MultiTrigger而使用Trigger
<MultiTrigger>
    <MultiTrigger.Conditions>
      <Condition Property="Validation.HasError" Value="true" />
      <Condition Property="IsEnabled" Value="true"  />
    </MultiTrigger.Conditions>
 <Setter .../>
</MultiTrigger>

因为我不知道它们...这个工作非常好。谢谢! - CitizenInsane

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接