开发者

C# WPF delete checked items in treeview

开发者 https://www.devze.com 2023-02-25 07:19 出处:网络
If i have a treeview like this: Book    -->items CD    -->items and every item contains a checkbox, how can i remove all the selected checkboxes.

If i have a treeview like this:

Book

   -->items

CD

   -->items

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

精彩评论

暂无评论...
验证码 换一张
取 消