启用或禁用上下文验证

启用或禁用上下文验证

本文介绍了启用或禁用上下文验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简介

我的视图中有两个TextBox,每个都绑定到我的视图模型中的某些属性(Property1Property2).

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

TextBox,并使用视图模型中的IDataErrorInfo +视图中的某些样式来验证属性.

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

问题

我想在禁用项目时禁用验证样式.

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

NB1:目前,我发现的解决方案是直接在视图模型中更改验证方案,但这需要通知属性更改,以强制视图重新读取IDataErrorInfo(而属性实际上并没有更改,只有选择器...)

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:我的问题确实很接近这个一个,但是描述和解决方案对我来说太复杂了真正理解要点.

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.

伪代码

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

推荐答案

为什么不使用MultiTrigger而不是Trigger:

<MultiTrigger>
    <MultiTrigger.Conditions>
      <Condition Property="Validation.HasError" Value="true" />
      <Condition Property="IsEnabled" Value="true"  />
    </MultiTrigger.Conditions>
 <Setter .../>
</MultiTrigger>

这篇关于启用或禁用上下文验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 21:51