GridMenuEventArgs.TargetElement Property
Gets the UI element for which the context menu is shown.
Namespace: DevExpress.Xpf.Grid
Assembly: DevExpress.Xpf.Grid.v24.2.Core.dll
NuGet Package: DevExpress.Wpf.Grid.Core
Declaration
Property Value
Type | Description |
---|---|
IInputElement | The element displayed within a View for which the context menu is shown. |
Remarks
Column Menu
The TargetElement property returns the GridColumnHeader object that represents the column header for which the context menu is shown.
Cell Menu
The TargetElement property returns the
DevExpress.Xpf.Grid.GridCellContentPresenter
object that represents the data cell for which the context menu is shown.GroupPanel Menu
The TargetElement property returns the ContentControl object that represents the Group Panel.
Summary Menu
The TargetElement property returns the
DevExpress.Xpf.Grid.GridTotalSummary
object.
Example
The following example demonstrates how to modify a column header in a grid’s Table View. This sample adds a button with an icon to a column header’s customization area.
Define a DataTemplate and assign it to the DataViewBase.ColumnHeaderCustomizationAreaTemplate property. This template specifies a button and its properties. For example, the context menu is invoked when you click the button, and the context menu is disabled when you right-click the column header or the custom button within it.
<dxg:GridControl.View>
<dxg:TableView Name="view" ShowGridMenu="TableView_ShowGridMenu">
<dxg:TableView.ColumnHeaderCustomizationAreaTemplate>
<DataTemplate>
<Button Tag="custom" Click="Button_Click" PreviewMouseRightButtonUp="Button_PreviewMouseRightButtonUp"
dxg:GridPopupMenu.GridMenuType="{x:Static dxg:GridMenuType.Column}">
<Button.Template>
<ControlTemplate TargetType="{x:Type Button}">
<Grid SnapsToDevicePixels="True" Background="Transparent">
<Path Name="path" Stretch="Fill" Fill="#FF0000FF"
Data="F1 M 1410.67,764.811L 1408.32,767.495C 1407.59,768.323 1406.36, 768.364 1405.59,767.586L 1405.41,767.414C 1404.64,766.636 1404.68, 765.407 1405.51,764.683L 1408.19,762.334C 1407.67,762.122 1407.1, 762 1406.5,762C 1404.01,762 1402,764.015 1402,766.5C 1402, 767.145 1402.14,767.755 1402.38,768.309L 1397.64,772.525C 1396.82, 773.257 1396.78,774.491 1397.56,775.269L 1397.73,775.44C 1398.51, 776.218 1399.74,776.182 1400.47,775.359L 1404.69,770.616C 1405.25, 770.86 1405.86,771.001 1406.5,771.001C 1408.98,771.001 1411, 768.985 1411,766.5C 1411,765.902 1410.88,765.333 1410.67,764.811 Z "
Width="14" Height="14" HorizontalAlignment="Center" VerticalAlignment="Center" />
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Fill" TargetName="path" Value="#FF00FF00" />
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Fill" TargetName="path" Value="#FFFF0000" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Button.Template>
</Button>
</DataTemplate>
</dxg:TableView.ColumnHeaderCustomizationAreaTemplate>
</dxg:TableView>
</dxg:GridControl.View>
private void TableView_ShowGridMenu(object sender, GridMenuEventArgs e) {
// Check whether this event was raised for a column's context menu.
// If not - exit and leave the menu shown.
if (e.MenuType != GridMenuType.Column) return;
// Check whether this event was raised for a custom button.
// If not - exit and hide the menu.
if ((e.TargetElement as Control).Tag == null) {
e.Handled = true;
return;
}
}
private void Button_Click(object sender, RoutedEventArgs e) {
// Show the context menu when a custom button is clicked.
view.GridMenu.ShowPopup(sender as Button);
}
private void Button_PreviewMouseRightButtonUp(object sender, MouseButtonEventArgs e) {
// Don't show the context menu when a custom button is right-clicked.
e.Handled = true;
}