首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >有没有办法在ErrorTemplate中设置工具提示?

有没有办法在ErrorTemplate中设置工具提示?
EN

Stack Overflow用户
提问于 2013-02-28 01:43:46
回答 3查看 1.6K关注 0票数 2

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

代码语言:javascript
复制
<ResourceDictionary>
    <ControlTemplate x:Key="ErrorTemplate">
        <Border BorderThickness="1" BorderBrush="Red">
            <AdornedElementPlaceholder />
        </Border>
    </ControlTemplate>
</ResourceDictionary>

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

代码语言:javascript
复制
<TextBox Validation.ErrorTemplate="{DynamicResource ErrorTemplate}" />

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

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

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

EN

回答 3

Stack Overflow用户

发布于 2013-02-28 01:59:16

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

代码语言:javascript
复制
<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}" />

票数 1
EN

Stack Overflow用户

发布于 2013-02-28 02:42:52

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

代码语言:javascript
复制
<ControlTemplate x:Key="ErrorTemplate">
    <Border BorderThickness="1" BorderBrush="Red"
            Background="Transparent"
            ToolTip="{Binding Path=/ErrorContent}">
        <AdornedElementPlaceholder />
    </Border>
</ControlTemplate>
票数 0
EN

Stack Overflow用户

发布于 2013-03-01 21:49:22

我很抱歉我没有时间yesterday...Would你试试下面看看这是不是你想要的,好吗?

代码语言:javascript
复制
<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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15118706

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档