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.
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

how can ContextMenu command bind to ViewModel in MVVM style?

Thread ID:

Created:

Updated:

Platform:

Replies:

119959 Aug 18,2015 05:57 PM UTC Aug 19,2015 02:18 PM UTC WPF 1
loading
Tags: SfDataGrid
Tommie Chen
Asked On August 18, 2015 05:57 PM UTC

Hi,

I want to bind SfDataGrid ContextMenu MenuItem to a command from my viewmodel.  It doesn't work. I see the syncfusion sample code are using static command. Could you provide a sample which is not using static command?  Here is my code below,  Could you advice why this is not working? 

<syncfusion:SfDataGrid.RecordContextMenu>
                <ContextMenu Style="{x:Null}">
                    <MenuItem  Command="{Binding Path=DataContext.ShowParameterDetailCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}"
                                         CommandParameter="{Binding}"
                                      Header="Show Details">
                     </MenuItem>
                </ContextMenu>
            </syncfusion:SfDataGrid.RecordContextMenu>


C# side(use PRISM DelegateCommand):

this.ShowParameterDetailCommand = new DelegateCommand<object>(this.ShowParameterDetail);

public ICommand ShowParameterDetailCommand { get; private set; }

        private void ShowParameterDetail(object obj)
        {
 .....
        }

Jai Ganesh S [Syncfusion]
Replied On August 19, 2015 02:18 PM UTC

Hi Tommie,


Thank you for contacting Syncfusion Support.


We have analyzed your query with your code snippet. In that the DataContext is not set for ContextMenu items. Hence the context menu is not worked as expected. However you can achieve your requirement for binding the SfDataGrid ContextMenu MenuItem to a command from my viewmodel by setting Source for Context Menu Item,


Code Snippet [XAML]:


     <syncfusion:SfDataGrid.RecordContextMenu>

           <ContextMenu Style="{x:Null}">

              <MenuItem Command="{Binding CopyCommand,Source={StaticResource employeeInfoViewModel}}" Header="Copy"></MenuItem>

           </ContextMenu>

     </syncfusion:SfDataGrid.RecordContextMenu>




C#:


CopyCommand = new ContextMenuDemo.ContextMenuCommands.RelayCommand(ShowMessage);


private ICommand copyCommand;


public ICommand CopyCommand

{

    get

    {

       return copyCommand;

    }

    set

    {

       copyCommand = value;

    }

}


public void ShowMessage(object obj)

{

      //Do your actions here

}


We have also prepared the sample based on this which is available from the below location,


Sample: http://www.syncfusion.com/downloads/support/directtrac/142870/ze/ContextMenuDemo-1972320366


Please let us know if you need further assistance.


Thank you,

Jai Ganesh S


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.

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

;