【发布时间】:2014-10-24 08:15:09
【问题描述】:
我有这个TextBox:
<TextBox IsEnabled="{Binding IsChecked, ElementName=Cb_AllowDeletingPictures}"
Style="{DynamicResource TextBoxInError}">
<TextBox.Text>
<Binding Path="TimeBeforeDeletingPicture" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<helpers:TimeBeforeDeletingRule/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
TextBoxInError 样式包含 Validation.ErrorTemplate 属性的模板和设置工具提示并将 BorderBrush 和 Foreground 变为红色的 MultiTrigger,如下所示:
<Style x:Key="TextBoxInError" TargetType="{x:Type TextBox}">
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<DockPanel>
<TextBlock Foreground="Red" FontSize="20" FontWeight="Bold" Text="!" FontFamily="Segoe UI Light"></TextBlock>
<AdornedElementPlaceholder/>
</DockPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="Validation.HasError" Value="True"></Condition>
<Condition Property="IsMouseOver" Value="True"></Condition>
</MultiTrigger.Conditions>
<Setter Property="Foreground" Value="Red" />
<Setter Property="BorderBrush" Value="Red"/>
<Setter Property="ToolTip"
Value="{Binding (Validation.Errors)[0].ErrorContent, RelativeSource={x:Static RelativeSource.Self}}"/>
</MultiTrigger>
</Style.Triggers>
</Style>
现在,正如您在下一个屏幕截图中看到的那样,ErrorTemplate 工作正常,在 TextBox 前显示一个红色感叹号,问题在于:
- 当鼠标
is not overTextBox 时,Foreground 和 BorderBrush 都不是红色的,没关系。 - 现在当鼠标
is overTextBox 区域时,只有前景变为红色,而当鼠标在其上方时,BorderBrush 保持其原始行为。
当鼠标不在文本框区域时:
当鼠标在文本框区域上时:
为什么会有这样的行为,为什么它在 Foreground 而不是 BorderBrush 工作?我错过了什么吗?
【问题讨论】:
标签: c# .net wpf validation xaml