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. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Setting Source to Expander ContextMenu

Thread ID:

Created:

Updated:

Platform:

Replies:

147934 Sep 28,2019 05:51 AM UTC Oct 7,2019 01:01 PM UTC WPF 6
loading
Tags: SfTreeGrid
Michele
Asked On September 28, 2019 05:51 AM UTC

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



Susmitha Sundar [Syncfusion]
Replied On September 30, 2019 12:42 PM UTC

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  

Michele
Replied On October 1, 2019 07:28 AM UTC

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

Gowtham Gopalsamy [Syncfusion]
Replied On October 2, 2019 03:25 PM UTC

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 


Gowtham Gopalsamy [Syncfusion]
Replied On October 4, 2019 01:29 PM UTC

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 


Michele
Replied On October 4, 2019 02:42 PM UTC

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


Gowtham Gopalsamy [Syncfusion]
Replied On October 7, 2019 01:01 PM UTC

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 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon