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
close icon

how can ContextMenu command bind to ViewModel in MVVM style?

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)
        {
 .....
        }

1 Reply

JG Jai Ganesh S Syncfusion Team 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


Loader.
Live Chat Icon For mobile
Up arrow icon