gpt4 book ai didi

c# - 有没有办法在 ErrorTemplate 中设置工具提示?

转载 作者:太空宇宙 更新时间:2023-11-03 13:46:42 24 4
gpt4 key购买 nike

在我们的 WPF 应用程序中,我们有一个通用的控件模板,我们用它以一致的方式显示错误

<ResourceDictionary>
<ControlTemplate x:Key="ErrorTemplate">
<Border BorderThickness="1" BorderBrush="Red">
<AdornedElementPlaceholder />
</Border>
</ControlTemplate>
</ResourceDictionary>

在我们应用程序的其他地方,当控件可能显示错误时,我们会像这样设置 ErrorTemplate

<TextBox Validation.ErrorTemplate="{DynamicResource ErrorTemplate}" />

我现在想在此错误模板中显示工具提示,但是在边框上设置工具提示属性并没有太大帮助,因为工具提示仅在用户将鼠标悬停在 1px 宽的边框上时显示,而不是控件本身是错误的。

我知道我可以在样式中设置工具提示,但是这个错误模板应用于许多不同的控件(组合框等...)并且这些控件中的许多还使用独立于我的错误模板的样式 - 我真的希望能够以通用方式将我的错误模板应用于任何控件。

有什么方法可以在我的 ErrorTemplate 中设置工具提示?

最佳答案

我定义了一个样式。我的对象 (Customer) 上有 IDataErrorInfo,它对数据绑定(bind)到文本框的属性 (LastName) 进行验证。这是我的风格:

<Style x:Key="ValidationTextBox" TargetType="{x:Type Control}">
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Margin" Value="0,2,40,2"/>
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<DockPanel LastChildFill="True">
<Border Background="#B22222" DockPanel.Dock="Right" Margin="5,0,0,0" Width="20" Height="20" CornerRadius="10"
ToolTip="{Binding ElementName=customAdorner, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}">
<TextBlock Text="!" VerticalAlignment="Center" HorizontalAlignment="Center" FontWeight="Bold" Foreground="White"/>
</Border>
<AdornedElementPlaceholder Name="customAdorner" VerticalAlignment="Center">
<Border BorderBrush="#B22222" BorderThickness="1" />
</AdornedElementPlaceholder>
</DockPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style

<TextBox Style="{StaticResource ValidationTextBox}" Text="{Binding Path=Customer.LastName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, NotifyOnTargetUpdated=True, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}" />

关于c# - 有没有办法在 ErrorTemplate 中设置工具提示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15118706/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com