GOAL: I am trying to expand my treeview items using a single click. I need to access the DataContext
of selected node and set the property for expanding/collapsing.
What I have tried
I have put EventSetter
on TreeViewItem
for MouseLeftButtonUp
(MLBU) & PreviewMouseLeftButtonUp
. However, when a click on a leaf node, in the MLBU handler I get the Header property of sender as Parent and not leaf. The OriginalSource
is correctly pointing to leaf control. But if I use that I will have to determine all the type of Sources and then get the value out of it.
Is there any other way to accomplish this?
EDIT
XAML for setting the event:
<Style TargetType="TreeViewItem" BasedOn="{StaticResource SomeStyle}">
<Setter Property="IsExpanded" Value="{Binding Path=IsExpanded, Mode=TwoWay}"/>
<EventSetter Event="PreviewMouseLeftButtonUp" Handler="OnPreviewMouseLeftButtonUp"/>
<EventSetter Event="MouseLeftButtonUp" Handler="OnMouseLeftButtonUp"/>
</Style>
The treeview items have different datatemplate based on requirement. So in OriginalSource it can be either Image
or System.Windows.Documents.Run
(coming for Label
). Thus, I have to put if-else for each type of control I create. Whereas, my DataContext has top level interface for each item whose property I can set to expand it. So a generic conversion will work.