我如何确定焦点*现在*在哪里?
简短版本
public class CustomDataGrid : DataGridControl, IXmlSettingsProvider
{
public CustomDataGrid()
{
if (DesignModeHelper.IsInDesignMode) return;
Loaded += (e, a) =>
{
...
};
LostFocus += (e, a) =>
{
if(IsBeingEdited /* &&
CurrentFocusTarget.GetType() == typeof(TabControl)*/)
EndEdit();
};
}
}
如何在上面的示例中找到 UIElement CurrentFocusTarget
?
长版本 - 上下文
我们使用 XCeed DataGrid 来在延迟加载的 TabControl 的不同选项卡中显示数据。每个选项卡都是延迟加载的,因此仅当选项卡可见时才会呈现内容(更重要的是,会获取数据)。使用 MVVM 方法,整个数据流运行良好。
问题
不知何故,每当用户对单元格和“更改选项卡”进行更改时,数据绑定 ViewModel 属性(已更新)就会被设置为“null”。
为了避免这种情况,当焦点从网格中丢失时,可以调用 EndEdit()。
但是,我只想在 TabItem(或 TabControl)失去焦点时调用它。
所以,我的问题是:
从代码隐藏中了解焦点
现在在哪里的最简单方法是什么。我已经检查了 FocusManager,但似乎找不到当前的焦点持有者(或者焦点丢失的人)。
The short version
public class CustomDataGrid : DataGridControl, IXmlSettingsProvider
{
public CustomDataGrid()
{
if (DesignModeHelper.IsInDesignMode) return;
Loaded += (e, a) =>
{
...
};
LostFocus += (e, a) =>
{
if(IsBeingEdited /* &&
CurrentFocusTarget.GetType() == typeof(TabControl)*/)
EndEdit();
};
}
}
How can I find UIElement CurrentFocusTarget
in the above sample?
The long version - Context
We use the XCeed DataGrid in order to show data in different tabs of a lazy-loaded TabControl. Each tab is lazy-loaded so that the content gets rendered (and, more importantly, the data is fetch) only when the tab is Visible. The whole data flow works great using the MVVM approach.
The problem
Somehow, whenever the user makes changes to a cell and Changes Tab, the databound ViewModel property (that had already been updated) is being set to null
.
In order to avoid this, it is possible to call EndEdit() when the focus is lost from the grid.
However, I only want to call it when the focus is lost to a TabItem (or the TabControl).
So, my question is:
What is the easiest way to know where the Focus
is right now, from the codebehind. I have inspected the FocusManager
but can't seem to find the current Focus bearer (or to whom the focus is lost).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果 DataGrid 未托管在 Application.Current.MainWindow 中,我可能需要特殊情况。
这是完整的代码:
I might need a special case if the DataGrid is not hosted in the
Application.Current.MainWindow
.Here is the complete code: