2

我有更改 TreeViewItem 的选定子节点的 First/Last/Previous/Next 按钮。将 First 和 Last 节点设置为选中状态不是问题。例如,要选择最后一个子节点:

TreeViewItem selectedItem = (myTreeView.SelectedItem as TreeViewItem);
TreeViewItem ParentItem = (selectedItem.Parent as TreeViewItem);
(ParentItem.Items[ParentItem.Items.Count - 1] as TreeViewItem).IsSelected = true;

将上一个/下一个项目设置为选中的最简单/最优雅的方法是什么?

谢谢!

4

3 回答 3

5

您可以用 IndexOf 调用替换 for 循环。

int index = ParentItem.Items.IndexOf(MyTreeView.SelectedItem)

当然,最好检查 index + 1 是否是有效的集合索引。

对于前一个兄弟,它将是索引 - 1。

于 2009-01-20T19:38:59.740 回答
2

我的变体至少适用于 4 级树。

如果光标遇到最后一个 treeviewitem,它将在开始时进行。如果您在第一个节点按 Previous,您将留在原地。

        // Button Back
    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        if (treeViewStruct.Items.Count < 1) return;
        if (Currentitem == null)
        {
            ((TreeViewItem)treeViewStruct.Items[0]).IsSelected = true;
            return;
        }
        TreeViewItem item = FindPreviousItem(Currentitem);
        if (item == null)
            ((TreeViewItem)treeViewStruct.Items[0]).IsSelected = true;
        else item.IsSelected = true;
    }

    /// <summary>
    /// Get Previous Node
    /// </summary>
    /// <param name="item">Current selected node</param>
    /// <returns></returns>
    private static TreeViewItem FindPreviousItem(TreeViewItem item)
    {
        TreeViewItem foundedItem = null;
        var parentItem = (item.Parent as TreeViewItem);
        if (parentItem != null)
        {
            int index = parentItem.Items.IndexOf(item);
            if (index > 0 && index < parentItem.Items.Count)
            {
                return FindLastChildren((TreeViewItem)parentItem.Items[index - 1]);
            }
            else if (index == 0)
                return parentItem;
        }
        else return item;
        return foundedItem;
    }



    private static TreeViewItem FindLastChildren(TreeViewItem parent)
    {
        if (parent.Items.Count > 0)
        {
            return FindLastChildren((TreeViewItem)parent.Items.Last());
        }
        else return parent;
    }

    // вперед
    private void Button_Click_2(object sender, RoutedEventArgs e)
    {
        if (treeViewStruct.Items.Count < 1) return;
        if (Currentitem == null)
        {
            ((TreeViewItem)treeViewStruct.Items[0]).IsSelected = true;
            return;
        }
        TreeViewItem item = FindNextItem(Currentitem);
        if (item == null)
            ((TreeViewItem)treeViewStruct.Items[0]).IsSelected = true;
        else item.IsSelected = true;
    }


    /// <summary>
    /// Get Next Node
    /// </summary>
    /// <param name="item">Current selected node</param>
    /// <returns></returns>
    private static TreeViewItem FindNextItem(TreeViewItem item)
    {
        TreeViewItem foundedItem = null;
        if (item.Items.Count < 1)
        {
            var parentItem = (item.Parent as TreeViewItem);
            if (parentItem != null)
            {
                int index = parentItem.Items.IndexOf(item);
                if (index > 0 && index < parentItem.Items.Count - 1)
                {
                    return (TreeViewItem)parentItem.Items[index + 1];
                }
                else if (index == parentItem.Items.Count - 1)
                {
                    return FindFirstNextParent(parentItem);
                }
            }
            else return item;
        }
        return (TreeViewItem)item.Items[0];
    }

    private static TreeViewItem FindFirstNextParent(TreeViewItem parent)
    {
        var parentParent = ((TreeViewItem)parent).Parent;
        if (parentParent is TreeViewItem)
        {
            int pindex = ((TreeViewItem)parentParent).Items.IndexOf(parent);
            if ((pindex + 1) < ((TreeViewItem)parentParent).Items.Count)
                return ((TreeViewItem)((TreeViewItem)parentParent).Items[pindex + 1]);
            else
            {
                return FindFirstNextParent(((TreeViewItem)((TreeViewItem)parent).Parent));
            }
        }
        return parent;
    }
于 2011-05-21T11:04:28.787 回答
0

它并不优雅,但它确实有效。谁能想出一个更好的解决方案?(显示“下一个”功能)

int index = 0;
foreach (TreeViewItem i in ParentItem.Items) {
    if (i.Equals(MyTreeView.SelectedItem)) {
        break;
    }
    index++;
}
(ParentItem.Items[index + 1] as TreeViewItem).IsSelected = true;
于 2009-01-20T19:35:11.323 回答