【问题标题】:Is there any way to set a tooltip inside an ErrorTemplate?有没有办法在 ErrorTemplate 中设置工具提示?
【发布时间】:2013-02-13 15:36:48
【问题描述】:

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

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

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

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

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

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

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

【问题讨论】:

  • 很确定这无法完成 - 我所见过的以通用方式应用样式来完成此操作的最佳方法是 in this article

标签: c# wpf errortemplate


【解决方案1】:

我定义了一个样式。我的对象 (Customer) 上有 IDataErrorInfo,它对数据绑定到文本框的属性 (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

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

【讨论】:

  • 不幸的是,这仅在感叹号上显示工具提示,我希望为整个文本框显示工具提示(或任何有错误的控件)
【解决方案2】:

正如我在my answer here 中所说,您可以:

<ControlTemplate x:Key="ErrorTemplate">
    <Border BorderThickness="1" BorderBrush="Red"
            Background="Transparent"
            ToolTip="{Binding Path=/ErrorContent}">
        <AdornedElementPlaceholder />
    </Border>
</ControlTemplate>

【讨论】:

  • 这有助于显示工具提示,但也可以防止与底层控件的任何交互。我可以用 IsHitTestVisible="False" 解决这个问题,但不幸的是,我又回到了与以前完全相同的情况 - 没有工具提示!
【解决方案3】:

对不起,我昨天没有时间...你能在下面试试,看看这是不是你想要的,好吗?

<Style x:Key="ValidationTextBox2" TargetType="{x:Type Control}">
            <Setter Property="VerticalAlignment" Value="Center"/>
            <Setter Property="Validation.ErrorTemplate">
                <Setter.Value>
                    <ControlTemplate>
                        <Border BorderBrush="Red" BorderThickness="2">
                            <DockPanel LastChildFill="True" ToolTip="{Binding ElementName=customAdorner, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}" Background="Transparent">
                                <TextBlock />
                            <AdornedElementPlaceholder Name="customAdorner" VerticalAlignment="Center">
                            </AdornedElementPlaceholder>
                        </DockPanel>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

【讨论】:

    猜你喜欢
    • 2011-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多