0

我在silverlight Page中有一个Treeview。现在我必须在Treeview中以编程方式将节点添加到每个节点的子节点。我该怎么做?

我的代码是这样的

 TreeViewItem item1 = new TreeViewItem();
 item1.Header = "Products";
 item1.Items.Add(new TreeViewItem() { Header = "Media Players" });
 item1.Items.Add(new TreeViewItem() { Header = "Games" });
 item1.Items.Add(new TreeViewItem() { Header = "Charts" });
 MyTreeView1.Items.Add(item1);

现在这会给我这样的输出

Products
   Media Players    
   Games
   Charts

但我想要这样的输出

Products
   Media Players 
      Games
         Charts

意味着必须将节点添加到新创建节点的子节点..

4

1 回答 1

1

那这个呢?

    var products = new TreeViewItem {Header = "Products"};
    var mediaPlayers = new TreeViewItem() {Header = "Media Players"};
    var charts = new TreeViewItem() { Header = "Charts" };
    var games = new TreeViewItem() { Header = "Games" };

    products.Items.Add(mediaPlayers);
    mediaPlayers.Items.Add(games);
    games.Items.Add(charts);

MyTreeView1.Items.Add(products);
于 2012-11-02T09:43:56.637 回答