简体   繁体   English

刷新后聚焦TreeViewElement

[英]Focusing TreeViewElement after Refresh

I have a Problem with focusing an TreeViewItem. 我在聚焦TreeViewItem时遇到问题。 I have a (little bit optimated) TreeView and refreshing the content. 我有一个(有点优化)TreeView,并刷新了内容。 Visually the selected Item stays selected but logically it isn't. 从视觉上看,选定的项目保持选中状态,但从逻辑上讲则不是。 The Focus-Check at the beginning and at the end are not the same, but in my opinion they should. 焦点检查在开始和结束时并不相同,但我认为应该如此。

Here is my Code (with all non-working refocusing things): 这是我的代码(包含所有无法正常工作的重新调整内容):

private void Refresh(string selectedContent)
{
    //Check out the focused element
    //Returns "System.Windows.Controls.TreeViewItem Header:<my selectedContent-Value>; Items.Count:0"
    MessageBox.Show(Keyboard.FocusedElement.ToString());

    var currentFocus = Keyboard.FocusedElement;
    tv.Refresh(); //this refreshes the treeview

    //Not working
    Keyboard.Focus(currentFocus);

    //Not working
    DependencyObject focusScope = FocusManager.GetFocusScope(currentFocus);
    FocusManager.SetFocusedElement(focusScope, currentFocus);

    //also not working
    currentFocus.Focus();

    //not working
    Keyboard.Focus(tv.TryFindNode(selectedContent)); //TryFindNode searches the node in the TreeView and returns it

    //not working
    tv.TryFindNode(selectedContent).Focus();

    //Check out the focused element
    //returns "TreeViewTesting.TreeViewTesting" (my class where I'm testing this issue)
    MessageBox.Show(Keyboard.FocusedElement.ToString());
}

What I don't understand is, that this works: 我不明白的是,这有效:

private void Refresh(string selectedContent)
{
    //Check out the focused element
    //Returns "System.Windows.Controls.TreeViewItem Header:<my selectedContent-Value>; Items.Count:0"
    MessageBox.Show(Keyboard.FocusedElement.ToString());

    tv.Refresh(); //this refreshes the treeview

    MessageBox.Show("I'm just a Message to show a messagebox");

    tv.TryFindNode(selectedContent).Focus();

    //Check out the focused element
    //Returns "System.Windows.Controls.TreeViewItem Header:<my selectedContent-Value>; Items.Count:0"
    MessageBox.Show(Keyboard.FocusedElement.ToString());
}

So, how do I that, what the Dummy-Messagebox is doing with the focus? 那么,我该如何处理Dummy-Messagebox的焦点呢? Anyone has an Idea? 有人有主意吗?

I have an application that manipulates other controls after selecting a TreeNode in a TreeView. 我有一个在TreeView中选择TreeNode之后可以操纵其他控件的应用程序。 When the process is done, the TreeNode is still selected but the TreeView has lost focus. 该过程完成后,仍然选择TreeNode,但是TreeView失去了焦点。

Based on Hunv's approach in their comments above, I found the following to produce the desired result (which is TreeView focused, TreeNode selected): 基于Hunv在上面的评论中的方法,我发现以下内容可以产生所需的结果(针对TreeView,选择TreeNode):

private void treeView_AfterSelect(object sender, TreeViewEventArgs e)
{
    ProcessMySelection();

    // give the app a chance to 'recover' and keep focus on the TreeView
    // i don't understand why this works -- is there a better way?
    Application.DoEvents()
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM