ToolTip或者PopUp这个控件在做界面时会经常用到。如何对ToolTip进行自定义呢?

1.首先自定义tooltip的controlTemplate,完全清除系统默认效果, 如下:
            <ControlTemplate x:Key ="TooltipTemplate" TargetType="ToolTip">
                <ContentPresenter x :Name="contentPresenter" Height="{TemplateBinding Height }" Width="{ TemplateBinding Width}" ContentTemplate="{TemplateBinding ContentTemplate }" ></ContentPresenter>
            </ControlTemplate>
2.自定义tooltip的contentTemplate, 这样可以专注于tooltip的界面呈现, 而不关心tooltip要显示的字符串, 如下:
                <Style x :Key="ToolTipStyle" TargetType="ToolTip">
                     <Setter Property ="IsOpen" Value="False">
                </Setter>
               
                <Setter Property ="ContentTemplate">
                    <Setter.Value>
                        <DataTemplate>
                            <Border x :Name="errorBorder" Background="#CC595959" BorderBrush="#99000000" BorderThickness="1" CornerRadius ="3" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin ="0" MaxWidth="320">
                                <Border.Effect>
                                    <DropShadowEffect BlurRadius ="4" ShadowDepth="0"/>
                                </Border.Effect>
                                <Grid>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width ="Auto"/>
                                        <ColumnDefinition Width ="*"/>
                                    </Grid.ColumnDefinitions>
                                    <Border Margin ="16,16,8,16" VerticalAlignment="Top">
                                        <Path x :Name="path1" Grid.ColumnSpan="1" Data="M9.0789473,12.870737 L10.927632,12.870737 10.927632,14.5 9.0789473,14.5 z M9.0000001,5.9999999 L11,5.9999999 11,7.994543 10.526316,12.308322 9.4802633,12.308322 9.0000001,7.994543 z M9.9647158,1.8074455 C9.5912184,1.7923756 9.2860216,2.1402843 9.2860216,2.1402845 9.2860216,2.1402843 2.5977592,14.8926 2.2227221,15.46075 1.8476844,16.028899 2.5562553,16.218284 2.5562553,16.218284 2.5562553,16.218284 16.2035,16.218284 17.18278,16.218284 18.162063,16.218284 17.870029,15.460751 17.870029,15.460751 17.870029,15.460751 11.056506,2.8352754 10.494117,2.1197443 10.31837,1.8961406 10.134488,1.8142953 9.9647158,1.8074455 z M9.9331295,0.00021409988 C10.317457,0.0076069832 10.762559,0.20740509 11.244278,0.77299643 12.785778,2.5828881 19.97391,16.249695 19.97391,16.249695 19.97391,16.249695 20.318179,17.954408 18.505573,17.985971 16.692966,18.017535 1.5982747,17.985971 1.5982747,17.985971 1.5982747,17.985971 -0.27740097,18.206807 0.03512764,16.028899 0.3476572,13.850991 8.5362361,0.89893103 8.536236,0.8989315 8.5362361,0.89893103 9.0876089,-0.016049385 9.9331295,0.00021409988 z" Height="17" Stretch="Fill" Width="20" Visibility="Visible" Fill ="Red"/>
                                    </Border>
                                    <TextBlock x :Name="textBlock" Text="{TemplateBinding Content }" Margin="0,14,10,14" FontSize="14" Grid.Column ="1" TextWrapping="Wrap" Foreground="Red"/>
                                </Grid>
                            </Border>
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
                <Style.Triggers>
                    <Trigger Property ="IsOpen" Value="True">
                        <Trigger.EnterActions>
                            <BeginStoryboard>
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Duration="0:0:3">
                                        <DiscreteObjectKeyFrame KeyTime ="0:0:0">
                                            <DiscreteObjectKeyFrame.Value>
                                                <Visibility> Visible</Visibility >
                                            </DiscreteObjectKeyFrame.Value>
                                        </DiscreteObjectKeyFrame>
                                        <DiscreteObjectKeyFrame KeyTime ="0:0:3">
                                           <DiscreteObjectKeyFrame.Value>
                                                <Visibility> Hidden</Visibility >
                                           </DiscreteObjectKeyFrame.Value>
                                        </DiscreteObjectKeyFrame>
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </BeginStoryboard>
                        </Trigger.EnterActions>
                    </Trigger>
                </Style.Triggers>
            </Style>
3.应用Tooltip的style,如下:
     <Button Content ="TestButton">
            < Button.ToolTip>
                < ToolTip Style ="{ DynamicResource ToolTipStyle}" Content="This is a button"></ ToolTip >
            </ Button.ToolTip>
        </ Button>
4.以上style使用时的注意事项:
因为Animation的设置值优先于本地设置值,所以会出现ToolTip在动画结束时永远隐藏。
因此在需要打开tooltip时,首先要在动画开始时设置Visibility为Visible,第二要触发IsOpen=True则必须先IsOpen=False, 因为在Tooltip隐藏后并没有设置IsOpen=False。
代码如下:
                        _toolTip.Style = ToolTipStyle;
                        _toolTip.PlacementTarget = this.AssociatedObject;
                        _toolTip.Placement = PlacementMode.Bottom;
                        _toolTip.Content = toolTipText;
                        //must set false first to trigger storyboard
                        _toolTip.IsOpen = false;
                        _toolTip.IsOpen = true;
                        _toolTip.StaysOpen = false;
5.ToolTip的bug
对于ToolTip的显示延迟、显示时间长度、是否保持显示、显示时的位置等等,通用的做法都是通过ToolTipService来进行统一设置。比如:
      ToolTipService.InitialShowDelay="1000"
ToolTipService.ShowDuration="7000"
ToolTipService.BetweenShowDelay="2000"
ToolTipService.Placement="Right"
ToolTipService.PlacementRectangle="50,0,0,0"
ToolTipService.HorizontalOffset="10"
ToolTipService.VerticalOffset="20"
ToolTipService.HasDropShadow="false"
ToolTipService.ShowOnDisabled="true"
ToolTipService.IsEnabled="true"
但是,我发现在TextBox的PreviewInput事件里去弹出tooltip时, 以上的ToolTipService设置并没有起到效果,Tooltip的弹出位置和显示时间还是默认的, 即跟随鼠标显示,默认5s。
这个时候就需要对ToolTip的相关属性进行设置。
 _toolTip.Style = ToolTipStyle;
            _toolTip.PlacementTarget = this.AssociatedObject;
            _toolTip.Placement = PlacementMode.Bottom;
当然,当我们toolTip和ToolTipService都设置相关属性时, ToolTipService的设置优先。

WPF中ToolTip的自定义的更多相关文章

  1. wpf中ToolTip实现

    定义样式: <UserControl.Resources> <Style TargetType="DataGridCell" BasedOn="{Sta ...

  2. WPF中使用WindowChrome自定义窗口中遇到的最大化问题

    FrameWork 4.5 之后,内置了WindowChrome类,官方文档: https://msdn.microsoft.com/en-us/library/system.windows.shel ...

  3. 在WPF中自定义你的绘制(一)

    原文:在WPF中自定义你的绘制(一)   在WPF中自定义你的绘制(一)                                                                 ...

  4. WPF中实现自定义虚拟容器(实现VirtualizingPanel)

    WPF中实现自定义虚拟容器(实现VirtualizingPanel) 在WPF应用程序开发过程中,大数据量的数据展现通常都要考虑性能问题.有下面一种常见的情况:原始数据源数据量很大,但是某一时刻数据容 ...

  5. 在WPF中自定义你的绘制(五)

    原文:在WPF中自定义你的绘制(五) 在WPF中自定义你的绘制(五)                                                                   ...

  6. 在WPF中自定义你的绘制(三)

    原文:在WPF中自定义你的绘制(三) 在WPF中自定义你的绘制(三)                                                                  ...

  7. 在WPF中自定义你的绘制(四)

    原文:在WPF中自定义你的绘制(四)                                   在WPF中自定义你的绘制(四)                                 ...

  8. 在WPF中自定义你的绘制(二)

    原文:在WPF中自定义你的绘制(二)   在WPF中自定义你的绘制(二)                                                                 ...

  9. WPF中自定义绘制内容

    先说结论:实现了在自定义大小的窗口中,加载图片,并在图片上绘制一个矩形框:且在窗口大小改变的情况,保持绘制的矩形框与图片的先对位置不变. 在WinForm中,我们可以很方便地绘制自己需要的内容,在WP ...

随机推荐

  1. 【鸟哥的Linux私房菜】笔记

    操作系统核心的功能! 驱动程序与操作系统的关系 2. [计算机组成之组件] 3.CPU实际要处理的数据完全来自于主存储器,这是一个很重要的概念! 4.CPU是整个计算机系统最重要的部分,那么目前世界上 ...

  2. Kubernetes StatefulSets

    StatefulSets对于需要以下一项或多项的应用程序非常有用. 稳定,唯一的网络标识符. 稳定,持久的存储. 有序,优雅的部署和缩放. 有序,优雅的删除和终止. 有序的自动滚动更新. POD Id ...

  3. Linux下安装lrzsz上传下载工具

    使用yum安装 为什么要使用yum安装? 答:安装十分方便,几乎不需要别的操作,只需要一个yum命令就可以完成所有的安装过程. yum -y install lrzsz  要有网络才行 输入命令:rz ...

  4. MYSQL limit用法

    1.Mysql的limit用法 在我们使用查询语句的时候,经常要返回前几条或者中间某几行数据,这个时候怎么办呢?不用担心,mysql已经为我们提供了这样一个功能. SELECT * FROM tabl ...

  5. mysql启动报can't create/write to file 'var/run/mysqld/mysqld.pid 错误解决办法

    msql启动报错,启动不了. 进入mysql日志默认的路径为 /var/log/mysqld.log 查看日志,发现报错信息如下: can't create/write to file 'var/ru ...

  6. linux学习(rz和sz命令的安装和使用)

    lrzsz的安装 [root@spark1 ~]# yum install lrzsz rz用法 终端直接输入rz,出现文件选择对话框,选择要上传的文件就ok sz用法 下载filename文件: s ...

  7. hadoop 输出中文乱码问题

    本文转载至: http://www.aboutyun.com/thread-7358-1-1.html hadoop涉及输出文本的默认输出编码统一用没有BOM的UTF-8的形式,但是对于中文的输出wi ...

  8. nginx流量全copy记录

    参考:http://tyrion.iteye.com/blog/2311987 准备两台服务器: 0.0.0.1 0.0.0.2 在 0.0.0.1上 . 下载 wget https://github ...

  9. js添加后缀防止缓存

    jsp页面: 时间戳的话需要引入: <%@ page import="java.util.Date"%> <script type="text/java ...

  10. js建造者(生成器)模式

    建造者模式将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示. 在软件系统中,有时需要创建一个复杂对象,并且这个复杂对象由其各部分子对象通过一定的步骤组合而成. 建造者模式类图: ...