WPF Treeviewitem

本文关键字:Treeviewitem WPF | 更新日期: 2023-09-27 17:50:15

建议我有这个树视图:

 <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个树视图中,我动态地将树视图与复选框放在一起。

我正在尝试删除选中的项目,在我这样做的时刻:

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

WPF Treeviewitem

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

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);
}