Silverlight 4:如何在键盘焦点上显示ToolTip(修订版)

时间:2011-09-27 18:03:58

标签: silverlight event-handling keyboard focus tooltip

我原来的问题:

当项目获得键盘焦点时,是否有一种简单的方法可以显示工具提示,而不仅仅是鼠标悬停?我们有一个项目列表,其中包含用户可能会通过的工具提示,并且所需的行为也是为了显示工具提示。

添加了示例XAML。带有Tooltip设置的HyperlinkBut​​ton也需要键盘焦点。

    <DataTemplate x:Key="OfferingItemDT">
        <HyperlinkButton Command="{Binding Path=NavigateToLinkCommand}" ToolTipService.ToolTip="{Binding Tooltip}">                
             <Grid x:Name="gOfferingButtonRoot" Width="275" MaxHeight="78" Margin="5,3">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="40"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>

                <Image x:Name="imgServiceOfferingIcon" 
                Grid.RowSpan="2"            
                VerticalAlignment="Top" 
                Source="{Binding Path=Image, Converter={StaticResource ByteArrayToImageConverter}}" 
                Stretch="UniformToFill" 
                Margin="2,10,0,0"
                MaxHeight="32" MaxWidth="32"
                />
                <TextBlock x:Name="txbOfferingTitle"
                    Grid.Column="1"
                    Grid.Row="0"
                    Text="{Binding Title}"                               
                    TextWrapping="Wrap"                                        
                    Style="{StaticResource OfferingTileTitleText}"/>
                <TextBlock x:Name="txbOfferingDesc"
                Grid.Column="1"
                Grid.Row="1"
                Style="{StaticResource OfferingTileBodyText}"
                Text="{Binding BriefDescription}" />

             </Grid>
        </HyperlinkButton>
    </DataTemplate>             

更新: 根据{{​​3}}中的信息以及Anthony的评论,我在GotFocus事件处理程序中尝试了这段代码:

        private void showTooltip(object sender, RoutedEventArgs e)
    {
        HyperlinkButton hb = new HyperlinkButton();
        ToolTip ttip = new ToolTip();


        hb = sender as HyperlinkButton;


        ttip = ToolTipService.GetToolTip(hb) as ToolTip;
        ttip.IsOpen = true;            

    }

这似乎可行,但ttip始终为null。帮助

2 个答案:

答案 0 :(得分:1)

“简单”是主观用语。是的很容易。在您附加ToolTip的同一UI元素上,您可以挂钩GotFocusLostFocus事件处理程序,将使用ToolTipService.GetToolTip获取工具提示和集合{{1分别到IsOpentrue

答案 1 :(得分:0)

缺少的部分是在XAML中定义工具提示,以便我们可以访问Tooltip元素。

<HyperlinkButton MouseLeftButtonUp="showTooltip">
  <ToolTipService.ToolTip>
    <ToolTip>
      <TextBlock Text="My tooltip text"/>
    </ToolTip>
  </ToolTipService.ToolTip>
  <!-- ... -->
</HyperlinkButton>

背后的代码

private void showTooltip(object sender, RoutedEventArgs e)
{
  FrameworkElement frameworkElement = (FrameworkElement)sender;
  ToolTip tooltip = ToolTipService.GetToolTip(frameworkElement) as ToolTip;
  if (tooltip != null)
  {
    tooltip.IsOpen = true;
    frameworkElement.MouseLeave += new MouseEventHandler(frameworkElement_MouseLeave);
  }
}

static void frameworkElement_MouseLeave(object sender, MouseEventArgs e)
{
  FrameworkElement frameworkElement = (FrameworkElement)sender;
  frameworkElement.MouseLeave -= new MouseEventHandler(frameworkElement_MouseLeave);

  ToolTip tooltip = ToolTipService.GetToolTip(frameworkElement) as ToolTip;
  if (tooltip != null)
  {
    tooltip.IsOpen = false;
  }
}