Finding out which treeview node was clicked from context menu.

Sep 20, 2012 at 2:39 AM
Edited Sep 20, 2012 at 2:52 AM

On my treeview, opening up a contextmenu on a treeitem deselects the item. I'd like to find to get the node on where the user has right clicked then selected Open from.

          <TreeView Name="ProgramTree" ItemsSource="{Binding ProgramItemCollection}"

                               <TreeView.ContextMenu>
                                    <ContextMenu StaysOpen="True">
                                        <MenuItem Header="Open"
                                                  cal:Message.Attach="ProgramMenuItemOpen($this)"/>
                                        <MenuItem Header="CheckSyntax"/>
                                    </ContextMenu>
                                </TreeView.ContextMenu>

 

        public void ProgramMenuItemOpen(object item)
        {
            MessageBox.Show(item.ToString());
        }

 

One solution would be to handle previewmousedown event and programmatically focus the node like shown here http://stackoverflow.com/questions/592373/select-treeview-node-on-right-click-before-displaying-contextmenu?rq=1 but this requires me to break the MVVM pattern.

Any suggestions?