C# WPF delete checked items in treeview
If i have a treeview like this:
Book
-->items CD -->itemsand every item contains a checkbox, how can i remove all the selected checkboxes.
Note: i don't use treenode.
I have a treeview with 3 treeview items(i.e book,cd and dvd). Dynamically i create nodes:
TreeViewItem newChild = new TreeViewItem();
newChild.Header = cbox开发者_运维问答;
if for example 5 treeviewitems are checked(for remove) then i want to remove them.
I think you need something like this:
void DeleteSelectedItems(TreeView root)
{
var todelete = List<TreeViewItem>();
GetSelectedItems(root.Items, todelete);
foreach(var it in todelete)
{
var parent = it.Parent;
parent.Items.Remove(it);
}
}
void GetSelectedItems(ItemsCollection tree, List<TreeViewItem> todelete)
{
foreach(var it in tree)
{
if (((it as TreeViewItem).Header as CheckBox).Checked)
todelete.Add(it);
else
GetSelectedItems(it.Items, todelete);
}
}
精彩评论