Help on HeaderContextMenu

Hi,

i need some help on implementing support for HeaderContextMenu using Prism and MVVM.
The code snippet on your help doesn't seem to work.

I want to have 2 sort of context menu. 1 for the Header and the other one the selected items in the grid.

This one is working fine:

      <syncfusion:SfDataGrid.ContextMenu>
        <ContextMenu>
          <MenuItem Header="Search on Google"
                    Command="{Binding ContextMenuGoogleSearchCommand}" 
                    CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=ContextMenu}, Path=PlacementTarget.SelectedItems}" >
          </MenuItem>
        </ContextMenu>
      </syncfusion:SfDataGrid.ContextMenu>


And i have this for the Header:

      <syncfusion:SfDataGrid.HeaderContextMenu>
        <ContextMenu>
          <MenuItem Command="{Binding ContextMenuClearFilterCommand}"
                    CommandParameter="{Binding}"
                  Header="Clear Filter">
          </MenuItem>
        </ContextMenu>
      </syncfusion:SfDataGrid.HeaderContextMenu>


While the first calls my command in my ViewModel, the context menu for the Header is shown, but the command is not invoked.

i have seen your sample, where you use the interactivity behavior to contruct the menu, but this would execute then outside my viewmodel.

any idea how i can make this work with my viewmodel?

thanks,

Helmut

3 Replies

VS Vijayarasan Sivanandham Syncfusion Team May 13, 2020 03:20 AM UTC

Hi Helmut Wahrmann,

Thank you for contacting Syncfusion support.

In ContextMenu Command binding, value is set as GridColumnContextMenuInfo. So you need to get the DataContext from DataGrid. Please refer the below code,
 
<Syncfusion:SfDataGrid.HeaderContextMenu> 
         <ContextMenu> 
                    <MenuItem Command="{Binding Path=DataGrid.DataContext.SortAscending}" 
                                          CommandParameter="{Binding}" 
                                          Header="Sorting Ascending"> 
                    </MenuItem> 
        </ContextMenu>                
</Syncfusion:SfDataGrid.HeaderContextMenu> 
KB Link: https://www.syncfusion.com/kb/2430/how-to-bind-the-itemsource-for-contextmenu-of-sfdatagrid

Regards,
Vijayarasan S



HW Helmut Wahrmann May 13, 2020 09:48 AM UTC

Hi Vijayarasan,

thanks for the info.
Problem solved.

Would be nice, if you would update the sample in your help:

There you are refering to:
Command="{Binding Source={x:Static Member=local:ContextMenuCommands.SortAscending}}"


VS Vijayarasan Sivanandham Syncfusion Team May 14, 2020 01:35 PM UTC

Hi Helmut Wahramnn,

Thanks for the update.

We are glad to know that the reported issue has been resolved. Please get back to us if you have any other queries. As always, we will happy to assist you😊

Regards,
Vijayarasan S 


Loader.
Up arrow icon