How to set Radial Menu as the context menu for WPF DataGrid?
RadialMenu (SfRadialMenu) can be used as context menu of the WPF DataGrid (SfDataGrid) by set the RadialMenu as ControlTemplate of ContextMenu. The same has been explained in the below “HeaderContext” style.
<Style x:Key="Headercontext" TargetType="ContextMenu">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<syncfusion:SfRadialMenu>
<syncfusion:SfRadialMenuItem Header="Ascending"
Command="{Binding Source={x:StaticMember=local:ContextMenuCommands.SortAscending}}"
CommandParameter="{Binding}"/>
<syncfusion:SfRadialMenuItem Header="Descending"
Command="{Binding Source={x:StaticMember=local:ContextMenuCommands.SortDescending}}"
CommandParameter="{Binding}"/>
</syncfusion:SfRadialMenu>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>#region SortAscending
static BaseCommand sortAscending;
public static BaseCommand SortAscending
{
get
{
if (sortAscending == null)
sortAscending = new BaseCommand(OnSortAscendingClicked,
CanSortAscending);
return sortAscending;
}
}
private static void OnSortAscendingClicked(object obj)
{
if (obj is GridColumnContextMenuInfo)
{
var grid = (obj as GridContextMenuInfo).DataGrid;
var column = (obj as GridColumnContextMenuInfo).Column;
grid.SortColumnDescriptions.Clear();
grid.SortColumnDescriptions.Add(new SortColumnDescription()
{
ColumnName = column.MappingName,
SortDirection = ListSortDirection.Ascending
});
}
}
private static bool CanSortAscending(object obj)
{
if (obj is GridColumnContextMenuInfo)
{
var grid = (obj as GridContextMenuInfo).DataGrid;
var column = (obj as GridColumnContextMenuInfo).Column;
var sortColumn = grid.SortColumnDescriptions.FirstOrDefault(x =>
x.ColumnName == column.MappingName);
if (sortColumn != null)
{
if ((sortColumn as SortColumnDescription).SortDirection ==
ListSortDirection.Ascending)
return false;
}
return grid.AllowSorting;
}
return false;
}
#endregion
#region SortDescending
static BaseCommand sortDescending;
public static BaseCommand SortDescending
{
get
{
if (sortDescending == null)
sortDescending = new BaseCommand(OnSortDescendingClicked,
CanSortDescending);
return sortDescending;
}
}
private static void OnSortDescendingClicked(object obj)
{
if (obj is GridColumnContextMenuInfo)
{
var grid = (obj as GridContextMenuInfo).DataGrid;
var column = (obj as GridColumnContextMenuInfo).Column;
grid.SortColumnDescriptions.Clear();
grid.SortColumnDescriptions.Add(new SortColumnDescription()
{
ColumnName = column.MappingName,
SortDirection =
ListSortDirection.Descending
});
}
}
private static bool CanSortDescending(object obj)
{
if (obj is GridColumnContextMenuInfo)
{
var grid = (obj as GridContextMenuInfo).DataGrid;
var column = (obj as GridColumnContextMenuInfo).Column;
var sortColumn = grid.SortColumnDescriptions.FirstOrDefault(x =>
x.ColumnName == column.MappingName);
if (sortColumn != null)
{
if ((sortColumn as SortColumnDescription).SortDirection ==
ListSortDirection.Descending)
return false;
}
return grid.AllowSorting;
}
return false;
}
#endregionConclusion
I hope you enjoyed learning about how to set RadialMenu as Context menu for DataGrid.
You can refer to our WPF DataGrid feature tour page to know about its other groundbreaking feature representations. You can also explore our WPF DataGrid documentation to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!