Silverlight 4:如何在键盘焦点上显示工具提示(修订)

发布于 2024-12-06 23:05:21 字数 2511 浏览 2 评论 0原文

我最初的问题:

当项目获得键盘焦点而不仅仅是鼠标悬停时,是否有一种简单的方法可以显示工具提示?我们有一个带有工具提示的项目列表,用户可能会通过选项卡浏览这些项目,并且期望的行为是也显示工具提示。

添加了示例 XAML。带有工具提示集的 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>             

更新: 根据 WPF: Show and persist ToolTip for a Textbox based on the cursor 中的信息以及 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。帮助?

My original question:

Is there an easy way for a ToolTip to be shown when an item gets keyboard focus, not just mouse over? We have a list of items with tooltips that users will probably tab through, and the desired behavior is for a tooltip to be shown then too.

Added example XAML. A HyperlinkButton with the Tooltip set is what needs the keyboard focus as well.

    <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>             

Updated:
Based on info in WPF: Show and persist ToolTip for a Textbox based on the cursor as well as Anthony's comments, I tried this code in the GotFocus eventhandler:

        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;            

    }

This seems like it would work, but ttip is always null. Help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青春如此纠结 2024-12-13 23:05:21

“容易”是主观术语。是的,这很容易。在附加 ToolTip 的同一 UI 元素上,您可以挂钩 GotFocusLostFocus 事件处理程序,它们将使用 ToolTipService.GetToolTip 获取工具提示,并将 IsOpen 分别设置为 truefalse

"Easy" is subjective term. Yes its easy. On the same UI element on which you attach the ToolTip you can hook the GotFocus and LostFocus event handler the will use ToolTipService.GetToolTip to acquire the tooltip and the set IsOpen to true and false respectively.

<逆流佳人身旁 2024-12-13 23:05:21

缺少的部分是在 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;
  }
}

The missing part is to define the tooltip in XAML so that we can access the Tooltip element.

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

Code behind

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;
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文