SilverLight控件之ToolTipService(用户提示信息)

ToolTipService与其说是个控件,如果把它作为属性,因为通常是把它放在其他的xaml元素之中。它为xaml控件提供弹出小窗口,小窗口中显示提示信息

重要属性:

  ToolTipService.Placement:Left、Right、Up、Down、Mouse(提示信息的位置)

  ToolTipService.ToolTip:提示信息。可以是字符串,也可以是其他xaml元素

  事例1

<Button x:Name="btn" Content="提交按钮" HorizontalAlignment="Center" VerticalAlignment="Center"
ToolTipService.Placement="Mouse" ToolTipService.ToolTip="提示" />

当鼠标进入按钮时,在鼠标的位置会弹出一个小窗口,小窗口显示:"提示"

  事例2

<Button x:Name="btn" Content="提交按钮" HorizontalAlignment="Center" VerticalAlignment="Center"
                ToolTipService.Placement="Mouse">
            <ToolTipService.ToolTip>
                <Image Source="/image/ooo.jpg"/>
            </ToolTipService.ToolTip>
        </Button>

当鼠标进入按钮时,在鼠标的位置弹出一个窗口,窗口中显示:图片

在代码中为某一控件添加ToolTip

事例:xaml中定义StackPanel,需要在代码中在StackPanel中添加树形结构,并为树形结构的每一个节点添加提示,提示鼠标坐在节点的简要信息

xaml代码:

1 <StackPanel Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Left" >
2                                         <telerik:RadTreeView  
3                                          BorderBrush="Gray"  BorderThickness="1" x:Name="MenuTree"
4                                                  Padding="2,3,10,2" VerticalAlignment="Stretch"
5                                                   Grid.Row="0"
6                                                  ImagesBaseDir="/images/topoicons/"  
7                                                  FontFamily="Arial,SimSun" FontSize="12" Width="170" Height="190"/>

想法:

1,在后台xaml代码中为StackPanel添加

  <ToolTipService.ToolTip>

    <TextBlock x:Name="text" Content=""/>

  </ToolTipService.ToolTip>

  为每一个TreeViewItem添加MouseEnter事件,当鼠标进入TreeViewItem时,动态改变text.Content的值。

但,ToolTipService是为控件产生提示。当鼠标进入控件时,提示框出现,并一直保留至鼠标离开控件。即,鼠标在控件内移动控件一直存在,且位置不变!!!

那么,如上述,如果为StackPanel添加提示,那么当鼠标进入该控件区域,提示出现,并直到鼠标离开控件。当鼠标在StackPanel中移动时,虽然tip内容发生改变,但tip位置不变。因此这种不行。(除非能让ToolTip随鼠标移动)

2,为每一个TreeViewItem添加ToolTip

ToolTipService.setToolTip(treeViewItem,"TreeViewItem简要信息");

但不知道为什么,同时不能设置tip的位置:ToolTipService.setPlacement(treeViewItem,PlacementModel.Mouse);

原文地址:https://www.cnblogs.com/lh-V/p/3428046.html