1

Hello i have a treeview which itemtemplate is checkbox. i want to get the selected items ischecked = true how can i do this?

I want to get all the items selected in a list. Some items may have sub items

 <TreeView x:Name="chkTree" ItemsSource="{Binding TreeRoot}">
                        <TreeView.ItemTemplate>
                            <HierarchicalDataTemplate DataType = "{x:Type local:CheckTreeSource}" ItemsSource = "{Binding Children}">
                                <StackPanel Orientation="Horizontal">
                                    <CheckBox IsChecked="{Binding IsChecked}"/>
                                    <TextBlock Text="{Binding Text}"/>
                                </StackPanel>
                            </HierarchicalDataTemplate>
                        </TreeView.ItemTemplate>
                    </TreeView>

UPDATE:

i tried this but this work only if a item is selected with sub items, But if only sub-item is selected (the item itself is not selected only some of its sub-items) This code does not work

foreach (var item in chkTree.Items)
            {
                if (((CheckTreeSource)item).IsChecked== true && ((CheckTreeSource)item).Children !=null)
                {
                    foreach (var subitem in ((CheckTreeSource)item).Children)
                    {
                        MessageBox.Show(((CheckTreeSource)subitem).Text + "");

                    }

                }
}
  • Have you tried anything? E.g. recursively iterating all items and returning those where IsChecked is true? Also note that IsChecked is not identical to whether an item is selected or not. – Clemens Jan 17 '20 at 11:05
  • @Clemens i updated my question please check –  Jan 17 '20 at 11:15
  • You are only iterating over child items when the parent item is checked. Why? You are also not iterating recursively, hence ignoring any grand child items etc. – Clemens Jan 17 '20 at 11:22
  • I'm trying to do something similar in Vue.js. This is the closest I got using recusivity: https://codepen.io/luizarusso/pen/zYxLOPb Maybe you can have some ideas by it's logic. I'm sure I iterate through all items but I'm not sure how to delete the selected one... doesn't work completely for me but might give you some help. – LuízaRusso Jan 17 '20 at 11:26

1 Answers1

1

You could have a static method that recursively adds checked items to a collection:

public static void AddCheckedItems(
    CheckTreeSource item, ICollection<CheckTreeSource> checkedItems)
{
    if (item.IsChecked)
    {
        checkedItems.Add(item);
    }

    if (item.Children != null)
    {
        foreach (var childItem in item.Children)
        {
            AddCheckedItems(childItem, checkedItems); // recursive call
        }
    }
}

Then call it with the root item as start:

var checkedItems = new List<CheckTreeSource>();

AddCheckedItems(TreeRoot, checkedItems);

Alternatively declare the method in the CheckTreeSource class:

public void AddCheckedItems(ICollection<CheckTreeSource> checkedItems)
{
    if (IsChecked)
    {
        checkedItems.Add(this);
    }

    if (Children != null)
    {
        foreach (var childItem in Children)
        {
            childItem.AddCheckedItems(checkedItems);
        }
    }
}

and call it like:

var checkedItems = new List<CheckTreeSource>();

TreeRoot.AddCheckedItems(checkedItems);

You may also want to take a look at this: https://stackoverflow.com/a/7063002/1136211

Clemens
  • 123,504
  • 12
  • 155
  • 268