如何在列表框中的类似ListView项目的TreeView中递归地找到按钮点击事件的treeviewitem的Next / Previous TreeViewItem?

时间:2021-11-26 23:15:00

Finding next and previous element of a TreeViewItem in a TreeView in WPF. Is it really possible to find a ListBox item in a ListView? because if I iterate it the next item moves to the next level not the sublevels of the the current level? can someone help me out here?

在WPF中的TreeView中查找TreeViewItem的下一个和上一个元素。是否真的可以在ListView中找到ListBox项?因为如果我迭代它,下一个项目会移动到下一个级别而不是当前级别的子级别?有人可以帮助我吗?

2 个解决方案

#1


1  

Here you have two methods for button clicks - moving up/down between siblings/children/parents:

这里有两种按钮点击方法 - 在兄弟姐妹/孩子/父母之间上下移动:

private void btnDown_Click(object sender, RoutedEventArgs e)
    {
        if (!(((TreeViewItem)myTree.SelectedItem).Parent is TreeView))
        {
            ItemCollection nodes = ((TreeViewItem)((TreeViewItem)myTree.SelectedItem).Parent).Items;

            for (int i = 0; i < nodes.Count; i++)
            {
                if (nodes[i].Equals(myTree.SelectedItem))
                {
                    try
                    {
                        if (i < nodes.Count - 1)
                        {
                            ((TreeViewItem)nodes[i + 1]).IsSelected = true;
                        }
                        else
                        {
                            if (((TreeViewItem)myTree.SelectedItem).Items.Count != 0)
                            {
                                ((TreeViewItem)((TreeViewItem)myTree.SelectedItem).Items[0]).IsSelected = true;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }

                    break;
                }
            }
        }
        else
        {
            if (((TreeViewItem)myTree.SelectedItem).Items.Count > 0)
            {
                ((TreeViewItem)((TreeViewItem)myTree.SelectedItem).Items[0]).IsSelected = true;
            }               
        }
    }

    private void btnUp_Click(object sender, RoutedEventArgs e)
    {

        if (!(((TreeViewItem)myTree.SelectedItem).Parent is TreeView))
        {
            ItemCollection nodes = ((TreeViewItem)((TreeViewItem)myTree.SelectedItem).Parent).Items;

            for (int i = 0; i < nodes.Count; i++)
            {
                if (nodes[i].Equals(myTree.SelectedItem))
                {
                    try
                    {
                        if (i > 0)
                        {
                            ((TreeViewItem)nodes[i - 1]).IsSelected = true;
                        }
                        else
                        {
                            ((TreeViewItem)((TreeViewItem)nodes[i]).Parent).IsSelected = true;
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }

                    break;
                }
            }
        }
    }

#2


0  

I had rather had a different approach to achieve the desired feature. followed the link here : [http://inputsimulator.codeplex.com][1]

我宁愿采用不同的方法来实现所需的功能。按照这里的链接:[http://inputsimulator.codeplex.com] [1]

and then included the binary in my project references.

然后在我的项目引用中包含二进制文件。

then click event of back and forward button, I sent keystroke UP and DOWN and achieved the required functionality.

然后点击后退和前进按钮的事件,我发送了按键UP和DOWN,并实现了所需的功能。

public void upbutton_click(object sender, RountedEventargs e)
{
  treeView1.Focus();
  Inputsimulater.SendKeys.SendWait("{UP}");
}

#1


1  

Here you have two methods for button clicks - moving up/down between siblings/children/parents:

这里有两种按钮点击方法 - 在兄弟姐妹/孩子/父母之间上下移动:

private void btnDown_Click(object sender, RoutedEventArgs e)
    {
        if (!(((TreeViewItem)myTree.SelectedItem).Parent is TreeView))
        {
            ItemCollection nodes = ((TreeViewItem)((TreeViewItem)myTree.SelectedItem).Parent).Items;

            for (int i = 0; i < nodes.Count; i++)
            {
                if (nodes[i].Equals(myTree.SelectedItem))
                {
                    try
                    {
                        if (i < nodes.Count - 1)
                        {
                            ((TreeViewItem)nodes[i + 1]).IsSelected = true;
                        }
                        else
                        {
                            if (((TreeViewItem)myTree.SelectedItem).Items.Count != 0)
                            {
                                ((TreeViewItem)((TreeViewItem)myTree.SelectedItem).Items[0]).IsSelected = true;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }

                    break;
                }
            }
        }
        else
        {
            if (((TreeViewItem)myTree.SelectedItem).Items.Count > 0)
            {
                ((TreeViewItem)((TreeViewItem)myTree.SelectedItem).Items[0]).IsSelected = true;
            }               
        }
    }

    private void btnUp_Click(object sender, RoutedEventArgs e)
    {

        if (!(((TreeViewItem)myTree.SelectedItem).Parent is TreeView))
        {
            ItemCollection nodes = ((TreeViewItem)((TreeViewItem)myTree.SelectedItem).Parent).Items;

            for (int i = 0; i < nodes.Count; i++)
            {
                if (nodes[i].Equals(myTree.SelectedItem))
                {
                    try
                    {
                        if (i > 0)
                        {
                            ((TreeViewItem)nodes[i - 1]).IsSelected = true;
                        }
                        else
                        {
                            ((TreeViewItem)((TreeViewItem)nodes[i]).Parent).IsSelected = true;
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }

                    break;
                }
            }
        }
    }

#2


0  

I had rather had a different approach to achieve the desired feature. followed the link here : [http://inputsimulator.codeplex.com][1]

我宁愿采用不同的方法来实现所需的功能。按照这里的链接:[http://inputsimulator.codeplex.com] [1]

and then included the binary in my project references.

然后在我的项目引用中包含二进制文件。

then click event of back and forward button, I sent keystroke UP and DOWN and achieved the required functionality.

然后点击后退和前进按钮的事件,我发送了按键UP和DOWN,并实现了所需的功能。

public void upbutton_click(object sender, RountedEventargs e)
{
  treeView1.Focus();
  Inputsimulater.SendKeys.SendWait("{UP}");
}