![](/img/trans.png)
[英]Tab/Enter/Arrow keys and control focus problems with WPF Datagrid and adjacent controls
[英]In WPF Datagrid, how to get ToolTip on keyboard focus using arrow keys?
我正在使用來自 codeplex 的 WPF DataGrid。 我為每個單元格附加了一個工具提示。 當鼠標懸停在單元格上時會出現此工具提示。
但是我可以為 Tooltip提供鍵盤功能嗎? 如果我使用向下箭頭或向上箭頭在 DataGrid 單元格之間移動(基本上是在單元格獲得焦點時),我希望工具提示可見。
制作自定義工具提示並嘗試以下操作:
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);
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.