2

I have a treeview bound to a Observable collection of some property type. There is a HierarchicalDataTemplate that shows the data in treeview. Now i need to show specific context menu for each HierarchicalDataTemplate item.

I am using the following XAML to show context menu:

<HierarchicalDataTemplate ItemsSource="{Binding Collections}">
            <TextBlock Text="{Binding Path=Name}">
            <TextBlock.ContextMenu>
                    <ContextMenu>
                        <MenuItem Header="Create" Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.AddCommand}" CommandParameter="{Binding}"/>
                    </ContextMenu>
                </TextBlock.ContextMenu>
            </TextBlock>
        </HierarchicalDataTemplate>

Here the AddCommand is written in the view model that is bound to this under control.. I am able to see the context menu, but event is not firing on click on menu item.

Please help..

H.B.
  • 166,899
  • 29
  • 327
  • 400
Arihant
  • 367
  • 1
  • 6
  • 22

1 Answers1

1

Your command binding will not work because the ContextMenu is not on the same logical tree as your UserControl is, therefore it will not find the UserControl ancestor. However your ContextMenu should inherit its container's datacontext automatically. So this should work -

<ContextMenu>
      <MenuItem Header="Create" Command="{Binding AddCommand}" CommandParameter="{Binding}"/>
</ContextMenu>

However the AddCommand property should exist on your HierarchicalDataTemplate bound item.

EDIT:

If your Command is not defined in your HierarchicalDataTemplate's bound item and instead in your UserControl. Then another think you may try is giving your UserControl a name, and then bind the command to it by ElementName. Like this

Updated again:

<ContextMenu>
      <MenuItem Header="Create" Command="{Binding ElementName="MyUserControl" Path="DataContext.AddCommand"}" CommandParameter="{Binding}"/>
</ContextMenu>
Dror
  • 2,548
  • 4
  • 33
  • 51
  • If the property of the AddCommand is not in your HierarchicalDataTemplate's bound item this will not work. Is this the case? – Dror Feb 27 '12 at 11:34
  • This dint work either. I am using MVVM pattern. The Add Command is defined in the View Model. – Arihant Feb 27 '12 at 16:19
  • Which view model? The data context inside the HierarchicalDataTemplate is not the main view model that you had defined for the usercontrol. However I updated my answer again. Try using the new one. – Dror Feb 27 '12 at 17:11
  • The view model for the User control which has the HierarchicalDataTemplate. Using the updated answer dint fire the event still. – Arihant Feb 28 '12 at 06:41
  • It's hard to know what exactly the problem is without more information. It would be nice if you could post relevant pieces of code from your UserControl's ViewModel where the command is defined. Also, did you try using snoop to diagnose the problem? – Dror Feb 28 '12 at 06:53