开发者

Silverlight 4: how to show ToolTip on keyboard focus (revised)

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 =开发者_StackOverflow sender as HyperlinkButton;


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

    }

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


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


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;
  }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜