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..

1 Answer 1

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>
Sign up to request clarification or add additional context in comments.

5 Comments

If the property of the AddCommand is not in your HierarchicalDataTemplate's bound item this will not work. Is this the case?
This dint work either. I am using MVVM pattern. The Add Command is defined in the View Model.
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.
The view model for the User control which has the HierarchicalDataTemplate. Using the updated answer dint fire the event still.
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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.