9 Answers, 1 is accepted
Please check out our test project. It basically shows a RadContextMenu in a RadtreeView. The DataContext of the RadTreeView is successfully passed to the RadContextMenu. RadTreeView uses the collection of DataItems from the MainViewModel and the ContextMenuItem uses the Command from the MainViewModel.
Please let us know if this is what you need.
Petar Mladenov
the Telerik team
Time to cast your vote for Telerik! Tell DevPro Connections and Windows IT Pro why Telerik is your choice. Telerik is nominated in a total of 25 categories.

I have placed my context menu inside the hierarchical template as I only want a context menu with certain items for that hierarchical item. But then I can only get the command to fire for the viewmodel of that item, not in the main viewmodel that the tree uses.
Regards,
Geoffrey
As I managed to understand you need the ContextMenu to appear only at a particular tree level. So I refactored this project to have three independent HierarchyTemplates like so:
<
HierarchicalDataTemplate
x:Key
=
"thirdLevelTemplate"
>
<
TextBlock
Text
=
"{Binding Name}"
/>
</
HierarchicalDataTemplate
>
<
HierarchicalDataTemplate
ItemsSource
=
"{Binding Children}"
x:Key
=
"secondLevelTemplate"
ItemTemplate
=
"{StaticResource thirdLevelTemplate}"
>
<
Grid
HorizontalAlignment
=
"Stretch"
>
<
TextBlock
Text
=
"{Binding Name}"
/>
<
telerik:RadContextMenu.ContextMenu
>
<
telerik:RadContextMenu
x:Name
=
"contextMenu"
>
<
telerik:RadMenuItem
Header
=
"InvokeCommand"
Command
=
"{Binding ShowMessageBoxCommand, Source={StaticResource viewModel}}"
/>
</
telerik:RadContextMenu
>
</
telerik:RadContextMenu.ContextMenu
>
</
Grid
>
</
HierarchicalDataTemplate
>
<
HierarchicalDataTemplate
ItemsSource
=
"{Binding Children}"
x:Key
=
"firstlevelTemplate"
ItemTemplate
=
"{StaticResource secondLevelTemplate}"
>
<
TextBlock
Text
=
"{Binding Name}"
/>
</
HierarchicalDataTemplate
>
Petar Mladenov
the Telerik team
Time to cast your vote for Telerik! Tell DevPro Connections and Windows IT Pro why Telerik is your choice. Telerik is nominated in a total of 25 categories.

Good morning,
I have the same problem, I have a different RadContextMenu for each DataTemplate and I need to bind Commands to ViewModel Commands but the concept of my application does not allow me to declare DataContext as StaticResource, because I need to call by code the constructor of the ViewModel from another window to specify some parameters.
How can I solve it in this case?
Regards
In order to bind RadMenuItem to an ICommand property from your view model, you can bind the Tag property of the control which wraps the RadContextMenu control to the parent data context (in your case RadTreeView). Then you can bind the Command property of the RadMenuItem to the Tag property. For your convenience, I have modified the code snippet from the Peter reply.
<
HierarchicalDataTemplate
x:Key
=
"thirdLevelTemplate"
>
<
TextBlock
Text
=
"{Binding Name}"
/>
</
HierarchicalDataTemplate
>
<
HierarchicalDataTemplate
ItemsSource
=
"{Binding Children}"
x:Key
=
"secondLevelTemplate"
ItemTemplate
=
"{StaticResource thirdLevelTemplate}"
>
<
Grid
HorizontalAlignment
=
"Stretch"
Tag
=
"{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type telerik:RadTreeView}}, Path=DataContext}"
>
<
TextBlock
Text
=
"{Binding Name}"
/>
<
telerik:RadContextMenu.ContextMenu
>
<
telerik:RadContextMenu
x:Name
=
"contextMenu"
>
<
telerik:RadMenuItem
Header
=
"InvokeCommand"
Command
=
"{Binding RelativeSource={RelativeSource Self},Path=Menu.UIElement.Tag.YourCommandName}}"
/>
</
telerik:RadContextMenu
>
</
telerik:RadContextMenu.ContextMenu
>
</
Grid
>
</
HierarchicalDataTemplate
>
<
HierarchicalDataTemplate
ItemsSource
=
"{Binding Children}"
x:Key
=
"firstlevelTemplate"
ItemTemplate
=
"{StaticResource secondLevelTemplate}"
>
<
TextBlock
Text
=
"{Binding Name}"
/>
</
HierarchicalDataTemplate
>
Give this approach a try and let me know if this works for you.
Regards,
Dinko
Telerik


Hello,
this solution works for me, although I get (sporadic) binding errors:
Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.FrameworkElement', AncestorLevel='1''. BindingExpression:Path=Parent; DataItem=null; target element is 'RadContextMenu' (Name=''); target property is 'Parent' (type 'DependencyObject')
Can you relate to that?
regards,
Tobias

Hi Peter,
I have tested your approach but wasn't able to reproduce it. May I ask you to share how you are adding new items and how you have set-up the RadTreeView?
Regards,
Dinko
Progress Telerik
Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.