Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 刷新后聚焦TreeView元素_C#_Wpf_Treeview_Focus - Fatal编程技术网

C# 刷新后聚焦TreeView元素

C# 刷新后聚焦TreeView元素,c#,wpf,treeview,focus,C#,Wpf,Treeview,Focus,我在聚焦TreeViewItem时遇到问题。 我有一个(有点优化)的树状视图和刷新的内容。 所选项目在视觉上保持选中状态,但在逻辑上不保持选中状态。开始时的焦点检查和结束时的焦点检查是不一样的,但我认为它们应该是相同的 这是我的代码(包括所有非工作的重新聚焦内容): private void刷新(字符串selectedContent) { //查看聚焦元素 //返回“System.Windows.Controls.TreeViewItem头:;项数:0” Show(Keyboard.Focuse

我在聚焦TreeViewItem时遇到问题。 我有一个(有点优化)的树状视图和刷新的内容。 所选项目在视觉上保持选中状态,但在逻辑上不保持选中状态。开始时的焦点检查和结束时的焦点检查是不一样的,但我认为它们应该是相同的

这是我的代码(包括所有非工作的重新聚焦内容):

private void刷新(字符串selectedContent)
{
//查看聚焦元素
//返回“System.Windows.Controls.TreeViewItem头:;项数:0”
Show(Keyboard.FocusedElement.ToString());
var currentFocus=Keyboard.FocusedElement;
tv.Refresh();//这将刷新树视图
//不起作用
键盘。焦点(currentFocus);
//不起作用
DependencyObject focusScope=FocusManager.GetFocusScope(当前焦点);
FocusManager.SetFocusedElement(聚焦镜、当前聚焦);
//也不起作用
currentFocus.Focus();
//不起作用
Keyboard.Focus(tv.TryFindNode(selectedContent));//TryFindNode在树视图中搜索节点并返回它
//不起作用
tv.TryFindNode(selectedContent.Focus();
//查看聚焦元素
//返回“TreeViewTesting.TreeViewTesting”(我测试这个问题的类)
Show(Keyboard.FocusedElement.ToString());
}
我不明白的是,这是有效的:

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());
}
private void刷新(字符串selectedContent)
{
//查看聚焦元素
//返回“System.Windows.Controls.TreeViewItem头:;项数:0”
Show(Keyboard.FocusedElement.ToString());
tv.Refresh();//这将刷新树视图
Show(“我只是一条显示MessageBox的消息”);
tv.TryFindNode(selectedContent.Focus();
//查看聚焦元素
//返回“System.Windows.Controls.TreeViewItem头:;项数:0”
Show(Keyboard.FocusedElement.ToString());
}
那么,我该怎么做,虚拟Messagebox对焦点做了什么?
有人有主意吗?

我有一个应用程序,在TreeView中选择一个TreeNode后可以操纵其他控件。当该过程完成时,TreeNode仍处于选中状态,但TreeView已失去焦点

根据Hunv在其上述评论中的方法,我发现以下方法可以产生预期的结果(以TreeView为重点,选择TreeNode):


好的,我知道了。我在本文中找到了提示:解决方案是“给树视图一些时间”,并使用它设置foucs:Dispatcher.BeginInvoke(DispatcherPriority.Input,newaction(()=>tv.TryFindNode(selectedContent.Focus());
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());
}
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()
}