What is the least intrusive way to display WPF hints in focus?

What is the minimum number of steps required to display a tooltip when the next control gains focus?

<TextBox ToolTip="Hello there!" ... />

I tried the following in gotfocus

    private void ..._GotFocus(object sender, RoutedEventArgs e) {
        var element = (FrameworkElement)sender;
        var tooltip = element.ToolTip;
        if (!(tooltip is ToolTip)) {
            tooltip = new ToolTip { Content = tooltip };
            element.ToolTip = tooltip;
        }

        ((ToolTip)tooltip).IsOpen = true;
    }

However, for this control, ToolTipService.Placementit ignores and SystemParameters.ToolTipPopupAnimationKeysets the level higher.

How can I make it work and follow all the settings that usually work for tooltips (except for time, obviously)?

+3
source share
2 answers

No need to test a Windows machine, but I would have thought:

<TextBox x:Name="textBox">
    <TextBox.ToolTip>
        <ToolTip IsOpen="{Binding IsKeyboardFocusWithin, ElementName=textBox}">
            Whatever
        </ToolTip>
    </TextBox.ToolTip>
</TextBox>
0
source

I built the IsKeyboardFocused binding in an attached property, for example:

 public class ShowOnFocusTooltip : DependencyObject
 {
   public object GetToolTip(...
   public void SetToolTip(...
   public static readonly DependencyProperty ToolTipProperty = DependencyProperty.RegisterAttached(..., new PropertyMetadata
   {
     PropertyChangedCallback = (obj, e) =>
     {
       ToolTipService.SetToolTip(obj,
         e.NewValue==null ? null :
         BuildToolTip(obj, e.NewValue));
     }
   });

   private object BuildToolTip(DependencyObject control, object content)
   {
     var tooltip = content is ToolTip ? (ToolTip)content : new ToolTip { Content = content };
     tooltip.SetBinding(ToolTip.IsOpenProperty,
       new Binding("IsKeyboardFocusWithin") { Source = control });
     return tooltip;
   }
0

Source: https://habr.com/ru/post/1743704/


All Articles