This is a migrated thread and some comments may be shown as answers.

Passing the RadGridView as a parameter to a command defined in a contextmenu in a style

1 Answer 479 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Hans
Top achievements
Rank 1
Veteran
Hans asked on 04 Apr 2018, 10:09 AM

Hi,

I'm trying to to accomplish the following: whenever a user right clicks whatever data grid, the same context menu should appear.  One of the options in the context menu is the export to Excel.  I'm trying to achieve this by creating a style for the RadGridView.  Here's my xaml:

<Style x:Key="TelerikGridViewContextMenuStyle" TargetType="{x:Type telerik:RadGridView}">
    <Setter Property="ContextMenu">
        <Setter.Value>
            <ContextMenu>
                <MenuItem Header="{bx:LocalizeBinding ExportToExcel, Global=True}" Command="{Binding ExportToExcelCmd}" CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor,  AncestorType={x:Type telerik:RadGridView}}}">
                    <MenuItem.Icon>
                        <Image Source="/Resources/Icons/Excel.png" />
                    </MenuItem.Icon>
                </MenuItem>
            </ContextMenu>
        </Setter.Value>
    </Setter>
</Style>

As you can see, I'm trying to pass the RadGridView as a parameter to the command.  However, in my code, this parameter is always null.  How come ?

 

1 Answer, 1 is accepted

Sort by
0
Hans
Top achievements
Rank 1
Veteran
answered on 04 Apr 2018, 12:24 PM

Ok, found it:

<Style x:Key="TelerikGridViewContextMenuStyle" TargetType="{x:Type telerik:RadGridView}">
    <Setter Property="ContextMenu">
        <Setter.Value>
            <ContextMenu>
                <MenuItem Header="{bx:LocalizeBinding ExportToExcel, Global=True}" Command="{Binding ExportToExcelCmd}" CommandParameter="{Binding Path=PlacementTarget, RelativeSource={RelativeSource Mode=FindAncestor, AncestorLevel=1,  AncestorType={x:Type ContextMenu}}}">
                    <MenuItem.Icon>
                        <Image Source="/Resources/Icons/Excel.png" />
                    </MenuItem.Icon>
                </MenuItem>
            </ContextMenu>
        </Setter.Value>
    </Setter>
</Style>

 

Tags
GridView
Asked by
Hans
Top achievements
Rank 1
Veteran
Answers by
Hans
Top achievements
Rank 1
Veteran
Share this question
or