2009-01-20 67 views
2

我有第一個/最後一個/上一個/下一個按鈕,用於更改TreeViewItem的選定子節點。將第一個和最後一個節點設置爲選中狀態不成問題。例如,選擇最後一個子節點:如何在TreeView中選擇「下一個/上一個TreeViewItem」?

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

什麼是設置一個/下一個項目爲被選擇的最簡單/最優雅的方式?

謝謝!

回答

5

您可以用IndexOf調用替換for循環。

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

當然,如果index + 1是一個有效的收集索引,當然會很好。

而對於前一個兄弟這將是指數 - 1

+0

工作就像一個魅力!謝謝! – Pwninstein 2009-01-20 19:47:37

0

這不是優雅,但它的工作原理。任何人都可以想出更好的解決方案嗎? (顯示「Next」功能)

int index = 0; 
foreach (TreeViewItem i in ParentItem.Items) { 
    if (i.Equals(MyTreeView.SelectedItem)) { 
     break; 
    } 
    index++; 
} 
(ParentItem.Items[index + 1] as TreeViewItem).IsSelected = true; 
2

我的變種對我的作品至少4級樹。

如果光標遇到最後一棵treeviewitem,它會在開始處。 如果您在第一個節點按「上一步」,則保持原位。

 // 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; 
    } 
相關問題