How can I check a sub item of an ObjectListView
programmatically?
CheckObject()
and CheckObjects()
work only with root items, not with sub items.
I tried to check it with the CheckSubItem()
method, but it doesn't work.
I also tried to load the children first and check them with Items[x].Checked = true
.
Example Code:
public void ReloadChecks(List<ExampleClass> toCheck)
{
List<ExampleClass> allProperties =
tvTreeView.Objects.Cast<ExampleClass>().ToList();
tvTreeView.CheckObjects(toCheck.Where(x => x.Parent == null));
foreach (ExampleClass subitem in toCheck.Where(x => x.Parent != null))
{
tvTreeView.CheckSubItem(subitem, tvTreeView.AllColumns[0]);
}
}
ExampleClass has a list of objects as children and an object as parent. Both attributes are nullable. The TreeView has an single columnheader: Checkbox | Name
I think there's a misunderstanding of "sub-items" going on here.
From your code, it seems you are dealing with a TreeListView
. Rows that are presented when you unroll a top-level object are "children" -- not "sub-items".
For a ListView
, "sub-items" are all the cells on a row except cell 0 (Microsoft's terminology, not mine).
So, this code successfully checks the top level objects:
tvTreeView.CheckObjects(toCheck.Where(x => x.Parent == null));
The following code does nothing since it tried to check a sub-item on the only item that cannot be sub-item (i.e. column 0):
tvTreeView.CheckSubItem(subitem, tvTreeView.AllColumns[0]);
To check all the children, but not the top level objects, you would just use CheckObjects()
again:
tvTreeView.CheckObjects(toCheck.Where(x => x.Parent != null));
CheckObjects()
works on any object in the control, not just top-level objects.