2012-11-21 45 views
0

我是WPF的新手,並嘗試使用treeview創建樹。關於WPF樹視圖的問題

我想要做的是動態生成一棵樹。每個treeViewItem都包含一個comboBox和一個textBlock。當用戶展開一個節點時,該應用程序將從數據源中檢索兒童節點信息。最後,用戶可以用複選框選擇幾個節點。

經過一些在線教程,我做了下面的樹:電子,如下圖所示:

<Window.Resources> 
    <Style TargetType="{x:Type TreeViewItem}"> 
     <Setter Property="HeaderTemplate"> 
      <Setter.Value> 
       <HierarchicalDataTemplate DataType="{x:Type sotc:TaxNode}" ItemsSource="{Binding Path=Children}"> 
        <StackPanel Orientation="Horizontal"> 
         <CheckBox Name="chk" Margin="2" Tag="{Binding}"/> 
         <TextBlock Text="{Binding Path=TaxID}" ToolTip="{Binding Path=Lineage}" /> 
        </StackPanel> 
       </HierarchicalDataTemplate> 
      </Setter.Value> 
     </Setter> 

    </Style> 
</Window.Resources> 

<Grid> 
    <TreeView Margin="25,186,22,46"> 
     <TreeViewItem Header="Taxonomy Tree" x:Name="_TaxTree" x:FieldModifier="private"> 
      <TreeViewItem Header="Loading..." TextBlock.FontStyle="Italic"></TreeViewItem> 
     </TreeViewItem> 
    </TreeView> 
</Grid> 

而且我有一個方法來獲得所選擇的組合框

private List<CheckBox> GetSelectedCheckBoxes(ItemCollection items) 
    { 
     var list = new List<CheckBox>(); 
     foreach (TreeViewItem item in items) 
     { 
      UIElement element = GetChildControl(item, "chk"); 
      if (element != null) 
      { 
       var chk = (CheckBox)element; 
       if (chk.IsChecked.HasValue && chk.IsChecked.Value) 
       { 
        list.Add(chk); 
       } 
      } 

      List<CheckBox> l = GetSelectedCheckBoxes(item.Items); 
      list = list.Concat(l).ToList(); 
     } 

     return list; 
    } 

    private UIElement GetChildControl(DependencyObject parentObject, string childName) 
    { 
     UIElement element = null; 
     if (parentObject != null) 
     { 
      int totalChild = VisualTreeHelper.GetChildrenCount(parentObject); 
      for (int i = 0; i < totalChild; i++) 
      { 
       DependencyObject childObject = VisualTreeHelper.GetChild(parentObject, i); 

       if (childObject is FrameworkElement && 
        ((FrameworkElement)childObject).Name == childName) 
       { 
        element = childObject as UIElement; 
        break; 
       } 

       // get its child 
       element = GetChildControl(childObject, childName); 
       if (element != null) break; 
      } 
     } 

     return element; 
    } 

但由於缺乏知識在WPF上,我不知道我應該傳遞給該方法的ItemCollection是什麼。

任何意見或教程將不勝感激。

有一個愉快的假期

回答

0

在XAML中,您可以將Name屬性添加到您的TreeView:

<Grid> 
    <TreeView Name="MyAwesomeTreeView" Margin="25,186,22,46"> 
     <TreeViewItem Header="Taxonomy Tree" x:Name="_TaxTree" x:FieldModifier="private"> 
      <TreeViewItem Header="Loading..." TextBlock.FontStyle="Italic"></TreeViewItem> 
     </TreeViewItem> 
    </TreeView> 
</Grid> 

現在,您現在可以訪問此樹視圖的項目在代碼隱藏這樣的:

ItemCollection myDataItems = MyAwesomeTreeView.Items 
GetSelectedCheckBoxes(myDataItems, MyAwesomeTreeView); 

但是,您顯示的方法在具有數據模板的TreeView上不起作用。原因是TreeView只會在屏幕上實際可見時纔會生成TreeViewItems。在此之前,它只包含基礎數據。爲了解決這個問題,你需要使用TreeView的ItemContainerGenerator。修改您的複選框方法爲:

private List<CheckBox> GetSelectedCheckBoxes(ItemCollection items, ItemsControl source) 
{ 
    var list = new List<CheckBox>(); 
    foreach (object dataitem in items) 
    { 
     UIElement treeviewitem = source.ItemContainerGenerator.ContainerFromItem(dataitem) 
     UIElement element = GetChildControl(treeviewitem, "chk"); 
     if (element != null) 
     { 
      var chk = (CheckBox)element; 
      if (chk.IsChecked.HasValue && chk.IsChecked.Value) 
      { 
       list.Add(chk); 
      } 
     } 

     List<CheckBox> l = GetSelectedCheckBoxes(item.Items, treeviewitem); 
     list = list.Concat(l).ToList(); 
    } 

    return list; 
} 

但我想強調的是,這不是一個好的做事方式。我敢打賭,你試圖達到的目標可以通過更簡單,直接和可維護的方式來實現,所以我建議你先解釋一下。

+0

感謝您的建議。我添加了名稱屬性,它運行但出現錯誤:{「無法投射'RNAMotifDescriptorGeneratorWPF.TaxNode'類型的對象來鍵入'System.Windows.Controls.TreeViewItem'。」}。有什麼建議麼? – Mavershang

+0

對不起,我沒有仔細查看原始代碼。 Items屬性只返回在XAML中聲明並且不使用ItemsSource和HiearchicalDataTemplate的TreeView的TreeViewItem集合。我編輯了我的答案.. – ValtsBlukis

+0

非常感謝詳細的解釋。在「列表 l = GetSelectedCheckBoxes(item.Items,treeviewitem);」中,item.Items是什麼? – Mavershang