Expand/Collapse TreeView Nodes on Ctrl+LeftMouse in WPF
-
I want to collapse or expand all TreeView nodes under parent node if user holds down Left Control key and presses left mouse button on expansion arrow of tree view. How do you do this in WPF? It's not as obvious as it was in WinForms.
-
I want to collapse or expand all TreeView nodes under parent node if user holds down Left Control key and presses left mouse button on expansion arrow of tree view. How do you do this in WPF? It's not as obvious as it was in WinForms.
Find an appropriate "hit" area / control and hook up the appropriate (preview) mouse event and e.Handled parameter. [How can i check if ctrl,alt are pressed on left mouse click in c#? - Stack Overflow](https://stackoverflow.com/questions/14178800/how-can-i-check-if-ctrl-alt-are-pressed-on-left-mouse-click-in-c)
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food
-
I want to collapse or expand all TreeView nodes under parent node if user holds down Left Control key and presses left mouse button on expansion arrow of tree view. How do you do this in WPF? It's not as obvious as it was in WinForms.
As Gerry mentioned, you need to hook up a handler for the mouse click and check the status of the CTRL key. You then go through all your child ViewModels and flip the property you databound to the IsExpanded property. This is how you can databind the IsExpanded property (and take IsSelected along for the ride if you need it - makes it VERY easy to select an item programmatically):
<Setter Property="IsExpanded" Value="{Binding Path=IsExpanded, Mode=TwoWay}"/> <Setter Property="IsSelected" Value="{Binding Path=IsSelected, Mode=TwoWay}"/>
In case you do not have a view model for your tree items you have two options: 1. Switch to WinForm 2. Introduce the view model I supposed you could also try calling methods on the tree view control itself to find it's children, but it will be a LOT more code than the viewmodel, specifically when it start delay loading and virtualizing etc. WPF was designed for a ViewModel, and going against that is always asking for pain.