2

In WPF I wanted to create a custom tooltip that, when open, could detect F1 key press, to take the user to a more detailed help file.

For reusability, my approach is to create a UserControl as a tooltip. The control will detect the KeyDown event and then execute a bindable Command.

But in practice the KeyDown event never appears to fire. Maybe Tooltips are not focusable for keyboard events? I have tried setting the KeyDown event for the UserControl, then for child controls inside the UserControl, no luck either way.

Here is (one example) of UserControl with KeyDown event:

<UserControl x:Class="HotKeyToolTip"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         KeyDown="UserControl_KeyDown">

Here is an example of how this control would be declared as a tooltip, in this case for the items of a combobox:

<ComboBox.ItemContainerStyle>
                    <Style>
                        <Setter Property="Control.ToolTip">
                            <Setter.Value>
                                <local:HotKeyToolTip Focusable="True"/>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </ComboBox.ItemContainerStyle>
Tekito
  • 840
  • 8
  • 24
  • Try `PreviewKeyDown` instead and see if that fires the event – Matt L. Jul 20 '17 at 20:38
  • Good idea but `PreviewKeyDown` did not appear to change things. I think the `Combobox` still has the focus in my example - key down on the right letter will switch between selected items. – Tekito Jul 20 '17 at 20:46

1 Answers1

0

It's difficult to handle KeyDown in ToolTip/UserControl, because if ToolTip will get focus, the combobox will loose it and so will close the dropdown AND ToolTip. You can take a look upon How to intercept all the keyboard events and prevent losing focus in a WinForms application?

I would handle KeyDown in Combobox and get/set tooltip text. For more flexibility I would implement it as a behavior!

        <ComboBox.ItemContainerStyle>
            <Style TargetType="Control">
                <Setter Property="Control.ToolTip">
                    <Setter.Value>
                        <ToolTip>
                            <local:HotKeyToolTip />
                        </ToolTip>
                    </Setter.Value>
                </Setter>
            </Style>
        </ComboBox.ItemContainerStyle>


private void ComboBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key==Key.F1)
            {
                var cmb = sender as ComboBox;
                var cmbi = cmb.Items.OfType<ComboBoxItem>().ToList();
                if (cmbi != null)
                {
                    foreach (var item in cmbi)
                    {
                        var tt = item.ToolTip as ToolTip;
                        if (tt != null && tt.IsOpen && tt.PlacementTarget == item)
                        {
                            (tt.Content as HotKeyToolTip).YourToolTipText = item.Content.ToString();//item.DataContext.ToolTipExtendedText
                        }
                    }
                }
            }
        }
Rekshino
  • 6,954
  • 2
  • 19
  • 44
  • That makes sense, and pretty much what I was doing before I tried to capture `keydown` in the `tooltip`. I went a step further and bound the `ToolTip.IsOpen` to my viewmodel, but even doing that was not a slam dunk. – Tekito Jul 21 '17 at 17:30