【问题标题】:Enabling or disabling validation upon context根据上下文启用或禁用验证
【发布时间】:2012-08-10 11:18:56
【问题描述】:

简介

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

TextBox 也可以在某些布尔值上启用,而属性则使用视图模型中的 IDataErrorInfo + 视图中的某些样式进行验证。

问题

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

NB1:目前,我找到的解决方案是直接在视图模型中更改验证方案,但这需要通知属性更改以强制视图重新读取IDataErrorInfo(而属性还没有真正改变了,只有选择器...)

NB2:我的问题与one 非常接近,但描述和解决方案过于复杂,我无法真正理解重点。

伪代码

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

【问题讨论】:

    标签: wpf xaml idataerrorinfo


    【解决方案1】:

    你为什么不用MultiTrigger 而不是Trigger

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

    【讨论】:

    • 因为我不知道他们......这就像一个魅力。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多