我有First / Last / Previous / Next按钮,用于更改TreeViewItem的选定子节点。将First节点和Last节点设置为选中不是问题。例如,要选择最后一个子节点:
TreeViewItem selectedItem = (myTreeView.SelectedItem as TreeViewItem);
TreeViewItem ParentItem = (selectedItem.Parent as TreeViewItem);
(ParentItem.Items[ParentItem.Items.Count - 1] as TreeViewItem).IsSelected = true;
将上一个/下一个项目设置为选中的最简单/最优雅的方法是什么?
谢谢!
答案 0 :(得分:5)
您可以使用IndexOf调用替换for循环。
int index = ParentItem.Items.IndexOf(MyTreeView.SelectedItem)
当然,检查index + 1是否是有效的集合索引会很好。
对于之前的兄弟姐妹,它将是索引-1。
答案 1 :(得分: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;
}
答案 2 :(得分: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;