We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Setting Source to Expander ContextMenu

Goodmorning
I'm developing  a UserControls Library.
I'm working on a UserControl based on sfTreeGrid called MyTreeView.

MyTreeView inherits Datacontext from MainWindow. Datacontext is ViewModel
In ViewModel there are many RelayCommands.
I need to assign via XAML those commands to a ContextMenu for Expander but i'm not able to assign the Source for each MenuItem.

This is my code:
<UserControl x:Class="MyControls.MyTreeView"
             x:Name="TreeViewUserControl"
               ......
<DockPanel>
     <Menu DockPanel.Dock="Top">
            <Button Command="{Binding CopyElement}" BorderBrush="Transparent" Background="Transparent">  //Here Command works
                <TextBlock  Text="Copy"/>
            </Button>
            .......
          <sf:SfTreeGrid ItemsSource="{Binding ItemSource, ElementName=TreeViewUserControl}" //Source  binded from CodeBehind via DependencyProperty

          <sf:SfTreeGrid.ExpanderContextMenu  >
                     <ContextMenu >
                              <MenuItem Command="{{Binding CopyElement, Source={RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}}" Header="Copy"  >

How can i bind CopyElement from ViewModel to MenuItem via XAML? 
I've tried many solutions, not only this one, without success.

Thanks 
Michele



6 Replies

SS Susmitha Sundar Syncfusion Team September 30, 2019 04:42 AM

Hi Michele,   
   
Thank you for using Syncfusion control.   
   
We have prepared the sample to achieve your requirement. In your sample, we have binded the command from ViewModel and set the source for that binding as ViewModel.   
Please refer the below code snippet.  
Xaml:   
<Window.Resources>   
        <local:ViewModel x:Key="viewModel"/>   
    </Window.Resources>   
   
<syncfusion:SfTreeGrid.ExpanderContextMenu>   
                <ContextMenu>   
                    <MenuItem Command="{Binding ExpandCommand, Source={StaticResourceviewModel}}"       
                                  CommandParameter="{Binding}"                                   
 Header="Expand" />   
                </ContextMenu>   
            </syncfusion:SfTreeGrid.ExpanderContextMenu>   
  
   
   
Please refer the UG documentation for more details.   
   
  
If we have misunderstood your requirement, please modify the sample based on your requirement and get back to us.   
  
Regards,   
Susmitha S  


MI Michele September 30, 2019 11:28 PM

Hello Sundar
Please, can you modify my code using  attached sample based on your provided sample?
SfTreeGrid_MVVM is a class library
SfTreeGridControls is a UsersContolLibrary
MainApp is a WPF desktop application

In SfTreeGridControls there is a UserControl, UserTreeGridControl, in wich i need to set datacontext inherited from MainApp.MainWindow when used in that Window.

For "classic" controls this works well: RelativeSource={RelativeSource AncestorType=UserControl}}
But in TreeGrid ContextMenu i can't use that xaml

Can you adapt your solution to my situation?

            <syncfusion:SfTreeGrid.ExpanderContextMenu>
                <ContextMenu>                   
                    
                    <!--How set the source from inherited MainWindow DataContext?-->                    
                    
                    <!--<MenuItem Command="{Binding ExpandCommand, Source={StaticResource viewModel}}"    
                                  CommandParameter="{Binding}"                                
                                  Header="Expand" />-->
                </ContextMenu>
            </syncfusion:SfTreeGrid.ExpanderContextMenu>

Attachment: SfTreeGridControl_90d47a03.zip


GG Gowtham Gopalsamy Syncfusion Team October 2, 2019 07:25 AM

Hi Michele, 
 
Thanks for using Syncfusion controls. 
 
We are analyzing your reported query. We will update more details on 04th October,2019. 
 
We appreciate your patience until then.      
Regards, 
Gowtham 



GG Gowtham Gopalsamy Syncfusion Team October 4, 2019 05:29 AM

Hi Michele,       
       
Thanks for your patience.  
  
We have modified the sample to achieve your requirement. In your sample, we have bind the ViewModel in MainApp and set the source for that binding as ViewModel. We have attached the modified sample in below link,  
  
  
Please let us know if you require further other assistance from us.    
  
Regards,           
Gowtham 



MI Michele October 4, 2019 06:42 AM

Hello Gowtham 
No problem for the delay but the solution is not as you write and is not what was expected from me.

You said: "....we have bind the ViewModel in MainApp and set the source for that binding as ViewModel... 
But this is not true
You have binded the dll directly to the usercontrol:

             xmlns:sr="clr-namespace:SfTreeGrid_MVVM"
             xmlns:tree="clr-namespace:SfTreeGrid_MVVM;assembly=SfTreeGrid_MVVM"
             xmlns:syncfusion="http://schemas.syncfusion.com/wpf"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <UserControl.Resources>
        <tree:ViewModel x:Key="viewModel"/>
    </UserControl.Resources>

                <ContextMenu>
                    <MenuItem Command="{Binding ExpandCommand, Source={StaticResource viewModel}}"    
                              CommandParameter="{Binding}"                                
                              Header="Expand" />
                </ContextMenu>

This is not what i need.
I need to inherit the datacontext from MainWindow in MainApp. I can not use dll (SfTreeGrid_MVVM) directly in the UserControl.
Please, can you provide a different solution?

Thanks 
Michele



GG Gowtham Gopalsamy Syncfusion Team October 7, 2019 05:01 AM

Hi Michele 
 
Thanks for your patience. 
 
You can achieve your requirement without using the dll’s and no need to inherit the datacontext from MainWindow in MainApp. You can use the below code to bind the ContextMenu. 
 
Please refer the below code snippet, 
 
            <syncfusion:SfTreeGrid.ExpanderContextMenu> 
                <ContextMenu> 
                    <MenuItem Command="{Binding TreeGrid.DataContext.ExpandCommand}"     
                                CommandParameter="{Binding}" Header="Expand" /> 
                </ContextMenu> 
            </syncfusion:SfTreeGrid.ExpanderContextMenu> 
 
 
Please refer the below sample link, 
 
 
Please let us know if you require further other assistance from us.       
 
Regards, 
Gowtham 


Loader.
Live Chat Icon For mobile
Up arrow icon