In WPF, how do I select the treeview item under my cursor on right-click? -


In WPF, when I right-click on a tree view item, I select it / active before showing the context menu I would like to be

It seems very simple, but the things included in a hierachicalDataTemplate are something

I have the following treeview tangled.

  & lt; TreeView X: Name = "TRV" ContextMenu = "{StaticResource ContextMenu}" ItemTemplate = "{StaticResource treeHierarchicalDataTemplate}" ItemsSource = "{Binding Source = {StaticResource meetingItems}}" & gt; & Lt; TreeView.ItemContainerStyle & gt; & Lt; Style TargetType = "{x: Type Tree VIIIIT}" & gt; & Lt; EventSetter Event = "Trivitim. Pravimous Right Buttondown" Handler = "TriviguWoumous Right Buttdowndown" /> & Lt; Setter Property = "IEPPended" value = "True" & gt; & Lt; / Setter & gt; & Lt; / Style & gt; & Lt; /TreeView.ItemContainerStyle> & Lt; / TreeView & gt;  

And here's my event handler ...

  Private Zero Trv_PreviewMouseRightButtonDown (Object Sender, MouseButtonEventArgs E) {TreeViewItem Item = TreeViewItem this as; If (item! = Null) {item.Focus (); E.Handled = True; }}  

Notice how I add an event setter above. It works perfectly but only selects the root-level tree view node (i.e. the original parent of the node on which I right click). Could this be due to my hierarchical data template? This template can contain children of the same type

Here is my hierarchical data template ...

   

Any idea why the root node is selected instead of the hair node when I right-click?

That's because the item sensor style is not inherited by the child nodes. . You need to add the same EventSetter to your HierarchicalDataTemplate O ItemContainerStyle

  & lt; HierarchicalDataTemplate x: key = "treeHierarchicalDataTemplate" ItemsSource = "{Binding Path = ChildMeetingItems}" & gt; & Lt; HierarchicalDataTemplate.Triggers & gt; & Lt; Data Trigger Binding = "{Binding Path = Red}" value = "true" & gt; & Lt; Setter target name = "img" property = "Image.Source" value = "pack: // siteoforigin: ;, / images / bookRed.png" & gt; & Lt; / Setter & gt; & Lt; / DataTrigger & gt; & Lt; /HierarchicalDataTemplate.Triggers> & Lt; StackPanel x: name = "triviewmpanel" background = "transparent" orientation = "horizontal" & gt; & Lt; Image width = "16" height = "16" x: name = "IMG" margin = "0,0,4,0" source = "pack: // cytoophorigine: ,,, images / bookGreen.png" & gt; ; & Lt; / Image & gt; & Lt; TextBlock foreground = "DarkGrey" text = "{binding performance index}" margin = "0,0,5,0" & gt; & Lt; / TextBlock & gt; & Lt; TextBlock Text = "{Binding Summary}" & gt; & Lt; / TextBlock & gt; & Lt; / StackPanel & gt; & Lt; HierarchicalDataTemplate.ItemContainerStyle & gt; & Lt; Style TargetType = "{x: Type Tree VIIIIT}" & gt; & Lt; EventSetter Event = "Trivitim. Pravimous Right Buttondown" Handler = "TriviguWoumous Right Buttdowndown" /> & Lt; / Style & gt; & Lt; /HierarchicalDataTemplate.ItemContainerStyle> & Lt; / HierarchicalDataTemplate & gt;  

Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -