自定义 WPF 工具提示 [英] Custom WPF tooltip

查看:42
本文介绍了自定义 WPF 工具提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个 WPF 工具提示,其中包含工具提示标题的标签,然后是包含更详细文本的文本块.我在资源字典中创建了以下样式:

I want to create a WPF tooltip containing a label for the header of the tooltip and then a textblock containing more detailed text. I've created the following style in a resource dictionary:

<Style x:Key="AppToolTip"
   TargetType="ToolTip">
<Setter Property="OverridesDefaultStyle" Value="true" />    
<Setter Property="Template">
        <Setter.Value>

            <ControlTemplate TargetType="ToolTip">
                <StackPanel>
                    <Label Content="{TemplateBinding Content}" FontWeight="Bold" Background="Blue" Foreground="White">

                    </Label>
                    <TextBlock Padding="10" TextWrapping="WrapWithOverflow" Width="200">

                    </TextBlock>
                </StackPanel>
            </ControlTemplate>
        </Setter.Value></Setter>
</Style>

并且可以成功地将此样式应用于这样的按钮并显示工具提示标题:

And can successfully apply this style to a button like so and have the tooltip header appear:

<Button.ToolTip>
<ToolTip Style="{DynamicResource PalletToolTip}">
                    <Binding Source="{x:Static ResStrings.New}"/>
                </ToolTip>
</Button.ToolTip>

我遇到的问题是如何根据上述用法设置额外描述性文本的内容?显示工具提示标题时,我已经将数据绑定到 Content 属性.任何阅读过 Adam Nathan 的 WPF Unleashed 一书的人都会认识到我使用的是他的示例工具提示 XAML,但在他的例子中,他使用硬编码字符串作为标签和文本块的内容.我想创建一些更可重用的东西,因此想使用数据绑定来实现相同的效果.

What i'm stuck on is how can i set the content of the extra descriptive text from the usage above ? I'm already data binding to the Content property when showing the tooltip header. Anyone who's read Adam Nathan's WPF Unleashed book will recognise that i'm using his example tooltip XAML but in his case, he's used hard coded strings for the content of the label and textblock. I want to create something that's more reusable and hence want to use data binding to achieve the same effect.

推荐答案

我将从 ToolTip 继承一个 HeaderedToolTip 类并添加一个 Header财产.我会像您所做的那样为该控件指定模板.然后我就可以像这样使用它:

I would inherit a HeaderedToolTip class from ToolTip and add a Header property. I would specify the template for that control much as you've done. Then I would be able to use it like so:

<Button>
    <Button.ToolTip>
        <HeaderedToolTip Header="My Title" Content="My Content"/>
    </Button.ToolTip>
</Button>

或者,使用绑定:

<Button>
    <Button.ToolTip>
        <HeaderedToolTip Header="{Binding ToolTipTitle}" Content="{Binding ToolTipText}"/>
    </Button.ToolTip>
</Button>

这篇关于自定义 WPF 工具提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆