介绍
我有两个TextBox
在我看来,每个属性都绑定到我的视图模型中的某些属性(Property1
, Property2
).
TextBox
或者在某些布尔值和属性上启用,并使用进行验证IDataErrorInfo
在视图模型中+视图中的一些样式。
Problem
我想在禁用项目时禁用验证样式。
NB1:目前,我发现的解决方案是直接在视图模型中更改验证方案,但这需要通知属性更改以强制视图重新读取IDataErrorInfo
(虽然属性并没有真正改变,只是选择器......)
NB2:我的问题非常接近这个one https://stackoverflow.com/q/7123687/684399,但是描述和解决方案对我来说太复杂了,无法真正理解要点。
伪代码
<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>