在WPF Datagrid中,如何使用箭头键在键盘焦点上获取ToolTip?

时间:2012-02-02 08:07:18

标签: c# wpf datagrid keyboard tooltip

我正在使用codeplex中的WPF DataGrid。我为每个单元格附加了一个工具提示。当鼠标悬停在单元格上时,会出现此工具提示。

但我可以为工具提示提供键盘功能吗?如果我使用向下箭头或向上箭头在DataGrid单元格之间移动(基本上当单元格获得焦点时),我希望工具提示可见。

请帮助我。

1 个答案:

答案 0 :(得分:0)

制作自定义工具提示并试试这个:

WPF解决方案:

  • XAML内容:

    <Button Canvas.Left="298" Canvas.Top="124" Height="34" 
      Name="button1"  Width="106" IsKeyboardFocusedChanged="showToolTip">
        Button
        <Button.ToolTip>
            <ToolTip>
                Whatever
            </ToolTip>
        </Button.ToolTip>
    </Button>
    
  • 通用事件处理程序:(因此所有控件都可以引用此事件处理程序,而不是为每个控件创建一个新事件)

  • 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解决方案:(我知道你没有要求它,但是我写了它,所以无论如何我都会发布它。)

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

    }
}