WPF Treeviewitem

时间:2011-04-14 11:58:41

标签: c# wpf

建议我有这个树视图:

 <TreeView Height="295" HorizontalAlignment="Left" Margin="11,58,0,0" Name="treeView1" VerticalAlignment="Top" Width="260" Grid.ColumnSpan="2">
            <TreeViewItem Header="Boek" Name="BoekenLijst" Width="260" HorizontalContentAlignment="Stretch" />
            <TreeViewItem Header="CD" Name="CDLijst" Width="260" />
            <TreeViewItem Header="DVD" Name="DVDLijst" Width="260" />
        </TreeView>

在3 Treeviewitems中,我动态地将treeviewitems放入其中的复选框。

我正在尝试删除所选项目,此时我这样做了:

TreeViewItem parent = treeView1.SelectedItem as TreeViewItem;
     //What do i need to put here so i can remove the selected childs?

1 个答案:

答案 0 :(得分:0)

如果要直接删除所选项目,请尝试:

treeView1.Items.Remove(treeView1.SelectedItem);

如果要删除所选项目的所有子项目:

ObservableCollection<TreeViewItem> helper = new ObservableCollection<TreeViewItem>();
foreach(TreeViewItem item in treeView1.SelectedItem.Items)
{
  helper.Add(item);
}

foreach(TreeViewItem item in helper)
{
  treeView1.SelectedItem.Items.Remove(item);
}