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

HeaderContextMenu in SfDataGrid derived class

Hello,

I have problems with a HeaderContextMenu in SfDataGrid. All worked fine until I derived SfDataGrid to an own class (MyDataGrid).

So in XAML instead of <syncfusion:SfDataGrid...> I use <local:MyDataGrid ...>

Other functionality works as expected but the HeaderContextMenu commands which are implemented with static commands do not work. The CanExecute() is called with obj = null instead of a GridContextMenuInfo type.

Excerpts from my code:

<ContextMenu x:Key="MyGridHeaderContextMenu">
<MenuItem Header="Sort ascending" Command="{Binding Source={x:Static Member=vw:GridContextMenuCommands.SortAscending}}" CommandParameter="{Binding}" />
...
</ContextMenu>


public static class GridContextMenuCommands
{
...
private static bool SortAscendingCanExecute(object obj)
{
if (obj is GridColumnContextMenuInfo)
{
var grid = (obj as GridContextMenuInfo).DataGrid;
...

Any hints?

Regards
Bernd


3 Replies

JG Jai Ganesh S Syncfusion Team November 10, 2016 05:59 AM UTC

Hi Bernd, 
We have prepared a sample for derived the SfDataGrid to custom class and create the HeaderContextMenu but its works fine in our end. 
<local:CustomSfDataGrid ItemsSource="{Binding GDCSource}" x:Name="datagrid"> 
 
            <local:CustomSfDataGrid.Columns> 
                <syncfusion:GridTextColumn MappingName="EmployeeName"/> 
                <syncfusion:GridTextColumn MappingName="EmployeeArea"/> 
                <syncfusion:GridTextColumn MappingName="EmployeeCity"/> 
                <syncfusion:GridTextColumn MappingName="EmployeeSalary"/> 
            </local:CustomSfDataGrid.Columns> 
 
            <local:CustomSfDataGrid.HeaderContextMenu> 
                <ContextMenu Style="{x:Null}"> 
                    <MenuItem Command="{Binding Source={x:Static Member=local:ContextMenucommand.SortAscending}}" 
                                      CommandParameter="{Binding}" 
                                      Header="Sort Ascending"/> 
                </ContextMenu> 
            </local:CustomSfDataGrid.HeaderContextMenu> 
</local:CustomSfDataGrid> 
 
Screen shot: 
 
 
Could you please check the above sample and let us know the details whether you have did any operations inside the customized SfDataGrid or if possible please replicate the issue in above sample. This would be more helpful for us to proceed further. 
Regards, 
Jai Ganesh S 



BE Bernd November 10, 2016 09:16 AM UTC

Jai,

thank you for your support. If ound the problem I had: I was assigning the ContextMenu from within the style which seems not to work on the derived control. The same applies to the GroupDropAreaStyle which cannot be assign from within a common style.

So this is NOT working:

<Style TargetType="{x:Type syncfusion:SfDataGrid}" x:Key="BaseGridStyle">
  <Setter Property="HeaderContextMenu" Value="{DynamicResource BaseGridHeaderContextMenu}"/>
  <Setter Property="GroupDropAreaStyle" Value="{DynamicResource BaseGridGroupDropAreaStyle}"/>

  <Setter Property="Background" Value="White"/>
</Style>

Any ideas how to change that?

And I encoutered a second problem: The ContextMenu no longer shows if the Grid is embedded in a TabControl and you switch the Tab and then go back to the Grid. I modified your sample to demonstrate the behaviour:

 <TabControl>
            <TabItem Header="Grid">
                <local:CustomSfDataGrid ItemsSource="{Binding GDCSource}" x:Name="datagrid">

                    <local:CustomSfDataGrid.Columns>
                        <syncfusion:GridTextColumn MappingName="EmployeeName"/>
                        <syncfusion:GridTextColumn MappingName="EmployeeArea"/>
                        <syncfusion:GridTextColumn MappingName="EmployeeCity"/>
                        <syncfusion:GridTextColumn MappingName="EmployeeSalary"/>
                    </local:CustomSfDataGrid.Columns>

                    <local:CustomSfDataGrid.HeaderContextMenu>
                        <ContextMenu Style="{x:Null}">
                            <MenuItem Command="{Binding Source={x:Static Member=local:ContextMenucommand.SortAscending}}"
                                          CommandParameter="{Binding}"
                                          Header="Sort Ascending"/>
                        </ContextMenu>
                    </local:CustomSfDataGrid.HeaderContextMenu>
                </local:CustomSfDataGrid>
            </TabItem>
            <TabItem Header="Second"/>
        </TabControl>

Why is that?

Best regards
Bernd


JG Jai Ganesh S Syncfusion Team November 11, 2016 08:53 AM UTC

Hi Bernd, 
A support incident to track the status of this query has been created under your account. Please log on to our support website to check for further updates 
Regards, 
Jai Ganesh S  


Loader.
Up arrow icon