[Question] How can I select multiple `TreeView` entries?
I am trying to implement a tree view with something similar to the approach Windows takes in explorer where every entry has a checkbox. It seems like by default that functionality is not yet available - is there a workaround that I can use to enable the tree view to use checkboxes?
This is related to the MultiSelect property, but with a different UI presentation.
There is quite a lot of versatility built into TreeView so something should be possible. How about this?

using Terminal.Gui;
using Terminal.Gui.Trees;
Application.Init();
var w = new Window();
// create tree view
TreeView treeView = new TreeView()
{
Width = 30,
Height = 3
};
// add some objects to the tree
var node1 = new TreeNode("Bob") {
Children = new List<ITreeNode>{
new TreeNode("Frank"),
new TreeNode("Peter")}
};
treeView.AddObject(node1);
// track what is checked in this collection
HashSet<ITreeNode> checkedObjects = new();
// customize how nodes are displayed based on whether they are selected
treeView.AspectGetter = o =>
{
return checkedObjects.Contains(o) ? $"[x] {o}" : $"[ ] {o}";
};
// register event handler to change tracked status
treeView.KeyPress += (obj) =>
{
if (obj.KeyEvent.Key == Key.Space)
{
var s = treeView.SelectedObject;
if (checkedObjects.Contains(s))
{
checkedObjects.Remove(s);
}
else
{
checkedObjects.Add(s);
}
treeView.SetNeedsDisplay();
}
};
w.Add(treeView);
Application.Run(w);
Application.Shutdown();
If you want to support multi select check/uncheck then you can replace SelectedObject with GetAllSelectedObjects() (and use a foreach loop)
@tznind although this is versatile I think this feature should be TreeView built-in, like the ListView does. Nonetheless, this is more difficult to be implemented on the TreeView than the LIstView, because TreeView has child nodes. Therefore, a multiple selection should have a three state checkbox.
- Checked square, if all the child nodes are selected.
- Empty square, if all the child nodes aren't selected.
- Solid square, if only some of the child nodes are selected.
So, the above sample will be always used if the user want to change the default text or color display. What do you think about this challenge?
@tznind although this is versatile I think this feature should be
TreeViewbuilt-in, like theListViewdoes. Nonetheless, this is more difficult to be implemented on theTreeViewthan theLIstView, becauseTreeViewhas child nodes. Therefore, a multiple selection should have a three state checkbox.* Checked square, if all the child nodes are selected. * Empty square, if all the child nodes aren't selected. * Solid square, if only some of the child nodes are selected.So, the above sample will be always used if the user want to change the default text or color display. What do you think about this challenge?
Its doable, theres also lots of customizations I can think of e.g.
- If you click a checkbox for a branch should it tick all leaf items
- Ticking a branch should tick unrevealed items the whole way down the tree even though
TreeView<T>does 'just in time' branch discovery. So methods like 'GetAllCheckedObjects' would just return the exposed ones.
Its pretty tough but doable.
Its doable, theres also lots of customizations I can think of e.g.
* If you click a checkbox for a branch should it tick all leaf items
Yes, if the branch isn't selected or only some leaf items are selected (solid state on the branch), clicking a checkbox it will mark all leaf items. On the other hand, if the branch is already selected clicking a checkbox will unmark all the leaf items.
* Ticking a branch should tick unrevealed items the whole way down the tree even though `TreeView<T>` does 'just in time' branch discovery. So methods like 'GetAllCheckedObjects' would just return the exposed ones.
I agree, Should tick or unpick unrevealed items the whole way down the tree, dependent of the current state. Method only have to return the exposed ones (expanded).
Its pretty tough but doable.
I know, thanks for your attention. To be more tough, only display the checkbox if a property like ShowCheckbox or similar is enable. If MultiSelect and ShowCheckbox properties are enabled it acts like the above. If MultiSelect is enabled and ShowCheckbox is disabled, then the selected items will be displayed by the selected or u-selected color. If MultiSelect is disabled and ShowCheckbox is enabled clicking a branch checkbox will not tick all leaf items, but only the branch itself. If another item is clicked then the previous item must be deselected. If MultiSelect and ShowCheckbox properties are disabled it acts like currently does.
If all of this doesn't make any sense for you, then forget it 😃
Closing this question as I believe it was sufficiently answered.