我如何确定焦点*现在*在哪里?

发布于 2024-11-07 07:37:52 字数 1104 浏览 0 评论 0原文

简短版本

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

思念满溢 2024-11-14 07:37:52
FocusManager.GetFocusedElement(Application.Current.MainWindow)

如果 DataGrid 未托管在 Application.Current.MainWindow 中,我可能需要特殊情况。

这是完整的代码:

public class CustomDataGrid : DataGridControl, IXmlSettingsProvider
{
    public BSIDataGrid()
    {
        if (DesignModeHelper.IsInDesignMode) return;

        CommandBindings.Add(new CommandBinding(ResetDataGridLayout, ResetDataGridLayoutExecute, ResetDataGridLayoutCanExecute));

        Loaded += (e, a) =>
                      {
                          ...
                      };

        LostFocus += (e, a) =>
                         {
                             if(IsBeingEdited && IsTabFocused())
                                EndEdit();
                         };
    }

    private static bool IsTabFocused()
    {
        var dependencyObject = FocusManager.GetFocusedElement(Application.Current.MainWindow);
        return dependencyObject is TabItem;
    }
}
FocusManager.GetFocusedElement(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:

public class CustomDataGrid : DataGridControl, IXmlSettingsProvider
{
    public BSIDataGrid()
    {
        if (DesignModeHelper.IsInDesignMode) return;

        CommandBindings.Add(new CommandBinding(ResetDataGridLayout, ResetDataGridLayoutExecute, ResetDataGridLayoutCanExecute));

        Loaded += (e, a) =>
                      {
                          ...
                      };

        LostFocus += (e, a) =>
                         {
                             if(IsBeingEdited && IsTabFocused())
                                EndEdit();
                         };
    }

    private static bool IsTabFocused()
    {
        var dependencyObject = FocusManager.GetFocusedElement(Application.Current.MainWindow);
        return dependencyObject is TabItem;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文