Silverlight ContextMenu in Hierarchical Tree

Dec 14, 2010 at 12:44 PM

I've created a hierarchical tree control and I am adding a ContextMenu as part of the DataTemplate. I already saw a post that said that it won't bubble so I'm handling it on the item that I've bound to the tree. The problem that the events both seem to fire as soon as I right click and the context menu is displayed - it doesn't wait until the actual menu item is clicked.

Am I missing something here?

<win:HierarchicalDataTemplate x:Key="ItemsTemplate" ItemsSource="{Binding NavigationItems}">                       
        <TextBlock Text="{Binding Name, Mode=OneWay}" />
                <itk:ContextMenu  cal:Action.TargetWithoutContext="{Binding}">
                     <itk:MenuItem Header="Rename" cal:Message.Attach="Rename"/>
                     <itk:MenuItem Header="Disable" cal:Message.Attach="Disable" />

Dec 14, 2010 at 9:16 PM

Use longish message form & mention the event name. Use MouseLeftButtonUp (or down), it should work:

Message.Attach="[Event MouseLeftButtonUp] = [Action Rename]"

Dec 16, 2010 at 12:40 PM

That actually doesn't seem to work for me. It no longer hits the event handlers at all.

I did find a solution that is working for me in another thread (

I added this convention in my Bootstrapper:

ConventionManager.AddElementConvention<MenuItem>(MenuItem.ItemsSourceProperty, "DataContext", "Click");

Then in the view, I could just call it as a click:

   <itk:ContextMenu cal:Action.TargetWithoutContext="{Binding}">
      <itk:MenuItem Header="Rename" cal:Message.Attach="Rename($dataContext)" />
      <itk:MenuItem Header="Disable" cal:Message.Attach="DisableNode($dataContext)" />