This is a migrated thread and some comments may be shown as answers.

Context menu command binding in Hierarchical Template with MVVM

9 Answers 713 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Geoffrey
Top achievements
Rank 1
Geoffrey asked on 10 Sep 2012, 06:58 PM
I have a treeview with a hierarchical template and I'm using mvvm. So far I have not been able to get the command binding correct for the context menu. It's trying to bind to the item's datacontext, but I want to bind it to the datacontext of the treeview. I have tried several approaches including relativesource, etc. Any help would be appreciated.

 

9 Answers, 1 is accepted

Sort by
0
Petar Mladenov
Telerik team
answered on 13 Sep 2012, 02:31 PM
Hi Geoffrey,

 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.

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

0
Geoffrey
Top achievements
Rank 1
answered on 17 Sep 2012, 07:24 PM
Hi,

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 
0
Petar Mladenov
Telerik team
answered on 20 Sep 2012, 09:27 AM
Hello 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>
This way the ContextMenu appears only on the second level items. Command is bound to the MainViewModel's Command which is set as a local resource. Please let us know if this is what you needed.

Kind regards,
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.

0
Daniele
Top achievements
Rank 1
answered on 04 May 2016, 07:15 AM

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

0
Dinko | Tech Support Engineer
Telerik team
answered on 06 May 2016, 11:42 AM
Hello Daniele,

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
Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
0
Vlada
Top achievements
Rank 1
answered on 19 May 2020, 12:36 PM
Had lost a day trying in vain to find proper solution, how to properly bind commands in contextmenu items to datacontext of parent VM. Your solution to bind datacontext to Tag in template had finally gave me a hint how to resolve this. Thanks!
0
Peter
Top achievements
Rank 2
Iron
Iron
Veteran
answered on 25 Mar 2021, 11:16 AM

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

0
Peter
Top achievements
Rank 2
Iron
Iron
Veteran
answered on 25 Mar 2021, 11:54 AM
The Binding Error above is displayed when I add a new Item to my TreeView.
0
Dinko | Tech Support Engineer
Telerik team
answered on 29 Mar 2021, 10:24 AM

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.

Tags
TreeView
Asked by
Geoffrey
Top achievements
Rank 1
Answers by
Petar Mladenov
Telerik team
Geoffrey
Top achievements
Rank 1
Daniele
Top achievements
Rank 1
Dinko | Tech Support Engineer
Telerik team
Vlada
Top achievements
Rank 1
Peter
Top achievements
Rank 2
Iron
Iron
Veteran
Share this question
or