我有一扇窗户,像这样
<Window.Resources>
<ControlTemplate x:Key="ValidationTemplate">
<DockPanel>
<TextBlock Foreground="Red" FontSize="20">!</TextBlock>
<AdornedElementPlaceholder/>
</DockPanel>
</ControlTemplate>
<Style x:Key="TextBoxInError" TargetType="{x:Type TextBox}">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip"
Value="{Binding RelativeSource={x:Static RelativeSource.Self},
Path=(Validation.Errors)[0].ErrorContent}"/>
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<TextBox x:Name="SupportFolder"
Validation.ErrorTemplate="{StaticResource ValidationTemplate}"
Style="{StaticResource TextBoxInError}"
Padding="2">
<TextBox.Text>
<Binding Path="Preferences.SupportFolder" Mode="TwoWay" UpdateSourceTrigger="LostFocus">
<Binding.ValidationRules>
<local:FolderExistsValidationRule ValidationStep="RawProposedValue"/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>因此,我的印象是,当TextBox的输入出错时,TextBox的左侧会有一个小的红色感叹号,并且由于Style触发器,会有一个显示错误的ToolTip。ToolTip可以工作,但我看不到指示错误的感叹号。我从微软的示例中获取了这段代码,但我无论如何也看不出我在哪里犯了错。示例可以工作(Data Binding -> BindingValidation),但我似乎无法重现代码。会不会有人充当我的第二双眼睛,看看我做错了什么?
发布于 2021-04-27 00:12:31
您应该将错误模板中的TextBlock停靠在左侧或右侧:
<TextBlock Foreground="Red" FontSize="20" DockPanel.Dock="Right">!</TextBlock>使用您当前的标记,TextBlock将最终位于修饰元素的“之下”。
https://stackoverflow.com/questions/67249631
复制相似问题