确定谁在WPF窗口中具有焦点 [英] Determine who has focus in WPF Window

查看:217
本文介绍了确定谁在WPF窗口中具有焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用WPF并具有一个从DockingLibrary派生的窗口。该窗口具有一个网格,其中包含多个项目,其中一个是WPF数据网格。我们正在使用M-V-VM模式。创建并显示此windown时,没有选择该数据网格中的任何行。我们可以通过执行以下操作将行设置为突出显示:

We are using WPF and have a window derived from a DockingLibrary. This window has a grid with multiple items in it, one being a WPF datagrid. We are using the M-V-VM pattern. When this windown is created and shown, none of the rows in this datagrid are selected. We can set the row to display as highlighted by doing something like:

  SharedWindow.ShipmentWin.shipmentDataGrid.SelectedIndex = 0;

这会导致数据网格中的第一行显示为突出显示。但是,并非总是其中之一,该行未被选中,也没有焦点。我尝试将IsSelected设置为 Focus,并将焦点集中在此行上,如下所示:

This causes the first row in the datagrid to be shown as highlighted. But, and isn't there always one of these, this row is not Selected nor does it have Focus. I tried setting IsSelected and Focus on this row as in:

  SharedWindow.ShipmentWin.ShipVM.IsSelected = true;
  bool focused = SharedWindow.ShipmentWin.shipmentDataGrid.Focus();

我错了吗,还有其他方法可以选择datagrid中的第一行并专注于它?通常,当创建数据网格时,直到用户鼠标单击所需的行之前,都不会选择任何行。

Am I going about this all wrong and is there some other way to Select the first row in the datagrid and set focus to it? Typically, when a datagrid is created, no row is selected until the user mouse clicks on the desired row.

任何想法都将不胜感激。

Any thoughts would be greatly appreciated.

谢谢!

推荐答案

看看 FocusManager 。它允许您通过 SetFocusedElement 方法将焦点设置到另一个UI元素。此外,它还允许您确定应用程序中当前关注的元素,这对于调试焦点问题非常有用。 探听也可能有用。它在底部状态栏中显示了当前关注的元素。

Have a look at the FocusManager. It allows you the set the focus to another UI element via the SetFocusedElement method. Additionally, it allows you to determine the currently focused element in your application which is can get handy to debug focus issues. Snoop can be useful, too. It shows the currently focused element in the bottom status bar.

如果使用WPF工具包中的DataGrid,请准备好查找与焦点处理有关的错误。 最近已解决

If you use the DataGrid from the WPF Toolkit, be prepared to find some bugs in relation to focus handling. Some have been addressed recently.

这篇关于确定谁在WPF窗口中具有焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆