简体   繁体   中英

In WPF Datagrid, how to get ToolTip on keyboard focus using arrow keys?

I am using an WPF DataGrid from codeplex. I have attached a Tooltip for each cell. This tooltip appears when the mouse hovers over the cell.

But can I provide keyboardability to Tooltip . If I use down arrow or up arrow to move between the DataGrid cells(basically when the cell gets focus), I want the tooltip to be visible.

Make a custom ToolTip and try this:

WPF Solution:

  • XAML stuff:

     <Button Canvas.Left="298" Canvas.Top="124" Height="34" Name="button1" Width="106" IsKeyboardFocusedChanged="showToolTip"> Button <Button.ToolTip> <ToolTip> Whatever </ToolTip> </Button.ToolTip> </Button>
  • Generic Event Handler: (So all controls can reference this event handler rather than making a new one for each control)

  •  public void showToolTip(object sender, DependencyPropertyChangedEventArgs e) { //Get tooltip from sender. ToolTip tt = (ToolTip)(sender as Control).ToolTip; //Places the Tooltip under the control rather than at the mouse position tt.PlacementTarget = (UIElement)sender; tt.Placement = PlacementMode.Right; tt.PlacementRectangle = new Rect(0, (sender as Control).Height, 0, 0); //Shows tooltip if KeyboardFocus is within. tt.IsOpen = (sender as Control).IsKeyboardFocusWithin; }

WinForm Solution: (I know you didn't ask for it, but I had it written, so I'll post it anyways.)

public class myUserControls: UserControl
{
    [Category("Category for UserControl")]
    public class ToolTipAdv : ToolTip
    {
        public ToolTipAdv (IContainer container) : base(container)
        {
            this.AutomaticDelay = 300;
            this.BackColor = System.Drawing.SystemColors.Highlight;
            this.ForeColor = System.Drawing.Color.White;
        }

        public void SetToolTip(Control ctrl, string caption)
        {
            ctrl.GotFocus += ShowToolTip;
            base.SetToolTip(ctrl, caption);
        }
        public void ShowToolTip(object sender, EventArgs e)
        {
            string message = base.GetToolTip((Control)sender);
            base.Show(message, (IWin32Window)sender, (sender as Control).Location);
        }

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM