Skip to main content
A newer version of this page is available. .

How to: Customize a Context Menu for Field Values

  • 3 minutes to read

This example shows how to modify context menu for the Field, Data and Header areas in the PivotGridControl.

  • Field Value context menu contains two new items. One command enables the end-user to exclude all fields but the one which is hovered over. Another command copies the filed name to the clipboard.
  • Data Area context menu contains a command which enables the end-user to copy cell content to the clipboard.
  • Field Header context menu context menu is modified to remove a command which reorders fields.
  • Header Area context menu is modified to remove all built-in commands and add a drop-down menu with a single item which enables the end-user to show/hide column grand totals.

Custom Context Menu for PivotGrid Areas

Tip

A complete sample project is available in the DevExpress Code Examples database at http://www.devexpress.com/example=E2205.

?using DevExpress.Xpf.PivotGrid;
using System.Windows;

namespace WpfPivotGrid_CustomMenu
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            pivotGrid.DataSource = DataHelper.CreatePivotDataSource();
        }

        private void CopyFieldElementData_ItemClick(object sender, DevExpress.Xpf.Bars.ItemClickEventArgs e)
        {
            PivotGridFieldValueMenuInfo menuInfo = pivotGrid.GridMenu.MenuInfo as PivotGridFieldValueMenuInfo;
            if (menuInfo != null && menuInfo.FieldValueElementData != null &&
                menuInfo.FieldValueElementData.Value.ToString() != string.Empty)
            {
                Clipboard.SetDataObject(menuInfo.FieldValueElementData.Value);
            }
        }

        private void FilterFieldElementData_ItemClick(object sender, DevExpress.Xpf.Bars.ItemClickEventArgs e)
        {
            PivotGridFieldValueMenuInfo menuInfo = pivotGrid.GridMenu.MenuInfo as PivotGridFieldValueMenuInfo;
            if (menuInfo != null && menuInfo.FieldValueElementData != null &&
                menuInfo.FieldValueElementData.Value != null &&
                menuInfo.FieldValueElementData.Field != null)
            {
                PivotGridField field = menuInfo.FieldValueElementData.Field;
                object value = menuInfo.FieldValueElementData.Value;
                field.FilterValues.FilterType = FieldFilterType.Included;
                field.FilterValues.Add(value);
            }
        }

        private void CopyCellElementData_ItemClick(object sender, DevExpress.Xpf.Bars.ItemClickEventArgs e)
        {
            PivotGridCellMenuInfo menuInfo = pivotGrid.GridMenu.MenuInfo as PivotGridCellMenuInfo;
            if (menuInfo != null && menuInfo.CellElementData != null &&
                menuInfo.CellElementData.Value.ToString() != string.Empty)
            {
                Clipboard.SetDataObject(menuInfo.CellElementData.DisplayText);
            }
        }
    }
}