2011-04-07 9 views
0

정말이 문제를 해결할 수 없습니다.C# 도움말 treeview 및 체크 박스 내용

treeviews에 트리 뷰 항목이 있습니다. 트리보기 항목에는 내용이있는 체크 박스가 있습니다. 어떻게 콘텐츠를 가져 와서 목록에 넣을 수 있습니까? 현재 난 내가 제대로 질문을 얻는 경우에 확실하지 오전이

 foreach (TreeViewItem item in treeView1.Items) 
     { 


      foreach (TreeViewItem childItem in item.Items) 
      { 


       CheckBox checkBoxTemp = childItem.Header as CheckBox; 

       if (checkBoxTemp == null) continue; 

       optieListBox.Items.Add(checkBoxTemp.Content); 
      } 



     } 
+0

이 윈폼 또는 WPF인가? –

+0

treeView1.Items에서 추측하면 WPF입니다. –

답변

0

를 얻었으나,이 시도 할 수 있습니다.

 foreach (TreeViewItem childItem in item.Items) 
     { 
      CheckBox cbx = null; 
      //finds first checkbox 
      foreach(object child in childItem.Items){ 
       cbx = child as CheckBox; 
       if (cbx != null) break; 
      } 

      ctrList.Items.Add(cbx.Content); 
     } 
+0

오류가 발생하지 않음 – bubye

+0

추가 정보보다 많습니다. XAML 파일은 어떻게 생겼습니까? –

0

대신 TreeView를 컬렉션에 바인딩하십시오. 그렇게하면 데이터에 액세스하기 위해 UI 구성 요소를 조작 할 필요가 없기 때문에 데이터에 직접 액세스하게됩니다.

재귀를 통한 다른 방법 : 클래스 수준에서 optieListBox 목록을 선언하고 GetContainers() 메서드를 진입 점 호출로 호출합니다. optieListBox 목록은 treeview에있는 모든 체크 된 항목에 대한 컨텐츠 목록을 제공해야합니다.

List<string> optieListBox = new List<string>(); 

     private List<TreeViewItem> GetAllItemContainers(TreeViewItem itemsControl) 
     { 
      List<TreeViewItem> allItems = new List<TreeViewItem>(); 
      for (int i = 0; i < itemsControl.Items.Count; i++) 
      { 
       // try to get the item Container 
       TreeViewItem childItemContainer = itemsControl.ItemContainerGenerator.ContainerFromIndex(i) as TreeViewItem; 
       // the item container maybe null if it is still not generated from the runtime 
       if (childItemContainer != null) 
       { 
        allItems.Add(childItemContainer); 
        List<TreeViewItem> childItems = GetAllItemContainers(childItemContainer); 
        foreach (TreeViewItem childItem in childItems) 
        { 
         CheckBox checkBoxTemp = childItem.Header as CheckBox; 

         if (checkBoxTemp != null) 
          optieListBox.Items.Add(checkBoxTemp.Content); 

         allItems.Add(childItem); 
        } 
       } 
      } 
      return allItems; 
     } 

     private void GetContainers() 
     { 
      // gets all nodes from the TreeView 
      List<TreeViewItem> allTreeContainers = GetAllItemContainers(this.objTreeView); 
      // gets all nodes (recursively) for the first node 
      TreeViewItem firstNode = this.objTreeView.ItemContainerGenerator.ContainerFromIndex(0) as TreeViewItem; 
      if (firstNode != null) 
      { 
       List<TreeViewItem> firstNodeContainers = GetAllItemContainers(firstNode); 
      } 
     } 
+0

어떻게 treeview 컬렉션에 바인딩합니까 ??? – bubye

+0

가에 가지 않을 있다고 가정? – bubye

0

이 시도 : 트리 뷰의 노드가 아이 (노드)이있는 경우

또한
List<string> values = new List<string>; 
foreach (string node in treeView.Nodes) 
{ 
    values.Add(node); 
} 

//Loop through nodes 

,이 대신하려고 :

List<string> values = new List<string>; 

//Called by a button click or another control 
private void getTreeValues(Object sender, EventArgs e) 
{ 
    foreach (string node in treeView.Nodes) 
    { 
     TreeNode child = (TreeNode)child; 
     values.Add(node) 
     getNodeValues(child); 
    } 
    foreach (string value in values) 
    { 
     Console.WriteLine(value + "\n"); 
    } 
} 

//Recursive method which finds all children of parent node. 
private void getNodeValues(TreeNode parent) 
{ 
    foreach (string child in parent.Nodes) 
    { 
     TreeNode node = (TreeNode)child; 
     values.Add(child); 
     if (nodes.Nodes.Count != 0) getNodeValues(child); 
    } 
} 
+0

전혀 내 대답은 도움이 되었습니까 foreach는 (childItems에서 TreeViewItem ChildItem을을) : 내가 트 리뷰> TreeViewItem 책> TreeViewItem 책 항목 – zberk