1

I have a TreeView with some items in it. I also have a context menu on said TreeView.

At the moment, if you right click, it doesn't also select the item, which seems to be the default behaviour.

I want it to act like Windows, where right clicking on an item also selects that item.

Can someone help me with how to do this?

Thanks.

  • So I'm asking a question about something I don't know. You then ask why I don't do it myself? The answer to your question would be a lack of knowledge as to how to do it, hence the question. This is not a helpful reply. – TheFaithfulLearner Mar 27 '17 at 10:13
  • How do you know how much effort I've put into this? I've looked around the internet and StackExchange for the answer, but it seems the question words are just too general and aren't able to provide an exact answer. I'm afraid this negative attitude towards learners is a reason for a report. – TheFaithfulLearner Mar 27 '17 at 10:23
  • Have you tried assigning the context menu to tree items instead of the whole tree? – Emad Mar 27 '17 at 10:59

1 Answers1

0

Add an Trigger to your item, which sets IsSelected to true:

            <TreeView.Resources>
            <HierarchicalDataTemplate DataType="{x:Type YoutItemType}" ItemsSource="{Binding YourItemSource}">
                <StackPanel Orientation="Horizontal" Margin="2">
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="PreviewMouseRightButtonDown">
                            <ei:ChangePropertyAction PropertyName="IsSelected" Value="true" TargetObject="{Binding}"/>
                        </i:EventTrigger>
                    </i:Interaction.Triggers>

Or look at this question, there are more (maybe better) answers: Select TreeView Node on right click before displaying ContextMenu

Community
  • 1
  • 1
andreas
  • 301
  • 3
  • 6