2010-11-30 60 views
0

我想創建使用WPF樹的父子結構。WPF樹父子插入:

Tree 

->Parent 

    ->Child 

     ->Grand Child. 

我已經寫了下面的代碼,這是不能爲孩子插入。請幫我解決這個問題。

<Window x:Class="NewTree_DynamicNode.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="350" Width="525"> 
    <Grid> 
     <TreeView Name="treeFileSystem" TreeViewItem.Expanded="treeFileSystem_Expanded_1"> 
      <TreeViewItem Header="Categories" x:Name="_ImageTree" Tag="hi" 
        x:FieldModifier="private"> 
       <TreeViewItem TextBlock.FontStyle="Italic" 
        Header="Loading..." Name="treeFileSystem2"/> 
      </TreeViewItem> 
     </TreeView> 
    </Grid> 
</Window> 




private void treeFileSystem_Expanded_1(object sender, RoutedEventArgs e) 
     { 
      this._ImageTree = (TreeViewItem)e.OriginalSource; 
      this._ImageTree.Items.Clear(); 

      try 
      { 
       for(int i=0 ; i<2; i++) 
       { 
        TreeViewItem temp = new TreeViewItem(); 
        TreeViewItem temp1 = new TreeViewItem(); 
        temp.Header = "Parent"; 
        temp1.Header = "Child"; 
        temp.Items.Add(temp1);     
        this._ImageTree.Items.Add(temp);      
       } 
      } 
      catch 
      { 
        ///// 
      } 
     } 

回答

1

你的問題是,每次你展開一個節點,爲您展開節點的子節點總是「Parent

林不知道你想什麼來實現,但繼承人一些代碼:

TreeViewItem temp = new TreeViewItem(); 
temp.Header = "Child"; 
temp.Items.Add(null); 
this._ImageTree.Items.Add(temp); 

編輯 - 這是一個特定的名稱在層級

TreeViewItem temp = new TreeViewItem(); 
var header = string.Empty; 
switch (_ImageTree.Header.ToString()) 
{ 
    case "Categories": 
     header = "Parent"; 
     break; 
    case "Parent": 
     header = "Child"; 
     break; 
    case "Child": 
     header = "GrandChild"; 
     break; 
    default: 
     header = "Child of " + _ImageTree.Header; 
     break; 
} 
temp.Header = header; 
temp.Items.Add(null); 
this._ImageTree.Items.Add(temp); 
+0

嗨dean:當我點擊我的樹時,它應該與父母打開,當我點擊父母它應該打開與孩子等....我想要下面的樹結構樹--->父---->孩子-----> GrandChild – vrbilgi 2010-11-30 10:49:58