Skip to main content

DevExpress v24.2 Update — Your Feedback Matters

Our What's New in v24.2 webpage includes product-specific surveys. Your response to our survey questions will help us measure product satisfaction for features released in this major update and help us refine our plans for our next major release.

Take the survey Not interested

PivotGridControl.PopupMenuShowing Event

Allows you to customize the context menus.

Namespace: DevExpress.Xpf.PivotGrid

Assembly: DevExpress.Xpf.PivotGrid.v24.2.dll

NuGet Package: DevExpress.Wpf.PivotGrid

#Declaration

public event PopupMenuShowingEventHandler PopupMenuShowing

#Event Data

The PopupMenuShowing event's data class is PopupMenuShowingEventArgs. The following properties provide information specific to this event:

Property Description
Customizations Allows you to customize the context menu by adding new menu items or removing existing items.
Handled Gets or sets a value that indicates the present state of the event handling for a routed event as it travels the route. Inherited from RoutedEventArgs.
Items Gets a collection of items contained within the context menu.
MenuType Gets what kind of pivot grid element invokes the menu.
OriginalSource Gets the original reporting source as determined by pure hit testing, before any possible Source adjustment by a parent class. Inherited from RoutedEventArgs.
RoutedEvent Gets or sets the RoutedEvent associated with this RoutedEventArgs instance. Inherited from RoutedEventArgs.
Source Gets the PivotGridControl that raised the event.
TargetElement Gets the UI element for which the context menu is shown.

The event data class exposes the following methods:

Method Description
GetCellInfo() Returns information about the cell for which the menu is invoked.
GetFieldInfo() Returns information about the field for whose header the menu is invoked.
GetFieldValueInfo() Returns information about the field value for which the menu is invoked.
InvokeEventHandler(Delegate, Object) When overridden in a derived class, provides a way to invoke event handlers in a type-specific way, which can increase efficiency over the base implementation. Inherited from RoutedEventArgs.
OnSetSource(Object) When overridden in a derived class, provides a notification callback entry point whenever the value of the Source property of an instance changes. Inherited from RoutedEventArgs.

#Remarks

The PopupMenuShowing event occurs when an end-user invokes a context menu.

Use the event parameter’s PopupMenuShowingEventArgs.Customizations property to add new and remove existing menu items. This property provides access to a collection of actions that allow you to customize the context menu. To learn more, see Context Menus, Items and Links and Bar Actions.

To determine for what kind of pivot grid element (a cell, a field value, a field header or the field header area) the menu is invoked, use the PopupMenuShowingEventArgs.MenuType property. To gwt the actual visual element for which the menu is invoked, use the PopupMenuShowingEventArgs.TargetElement property.

Use the following methods to get more information about the visual element.

PopupMenuShowingEventArgs.GetCellInfo

Returns information about the cell for which the menu is invoked.

PopupMenuShowingEventArgs.GetFieldValueInfo

Returns information about the field value for which the menu is invoked.

PopupMenuShowingEventArgs.GetFieldInfo

Returns information about the field for whose header the menu is invoked.

#Example

This example demonstrates how to use the PivotGridControl.PopupMenuShowing event to add custom items to the built-in context menu.

pivotgrid-popupmenushowing-event

The event is handled automatically if the field’s AllowFieldSummaryTypeChanging or AllowFieldSummaryDisplayTypeChanging attached properties are true. The properties are defined in the HeaderMenuHelper class.

using DevExpress.Xpf.Bars;
using DevExpress.Xpf.PivotGrid;
using System;
using System.Reflection;
using System.Windows;

namespace HeaderMenuCustomizationExample
{
    class HeaderMenuHelper
    {
        #region AttachedProperties
        public static readonly DependencyProperty AllowFieldSummaryTypeChangingProperty =
               DependencyProperty.RegisterAttached("AllowFieldSummaryTypeChanging", typeof(Boolean), typeof(HeaderMenuHelper));
        public static void SetAllowFieldSummaryTypeChanging(DependencyObject element, Boolean value)
        {
            element.SetValue(AllowFieldSummaryTypeChangingProperty, value);
        }
        public static Boolean GetAllowFieldSummaryTypeChanging(DependencyObject element)
        {
            return (Boolean)element.GetValue(AllowFieldSummaryTypeChangingProperty);
        }

        public static readonly DependencyProperty AllowFieldSummaryDisplayTypeChangingProperty =
              DependencyProperty.RegisterAttached("AllowFieldSummaryDisplayTypeChanging", typeof(Boolean), typeof(HeaderMenuHelper));
        public static void SetAllowFieldSummaryDisplayTypeChanging(DependencyObject element, Boolean value)
        {
            element.SetValue(AllowFieldSummaryDisplayTypeChangingProperty, value);
        }
        public static Boolean GetAllowFieldSummaryDisplayTypeChanging(DependencyObject element)
        {
            return (Boolean)element.GetValue(AllowFieldSummaryDisplayTypeChangingProperty);
        }

        public static readonly DependencyProperty AllowPopupMenuCustomizationProperty =
              DependencyProperty.RegisterAttached("AllowPopupMenuCustomization", typeof(Boolean), typeof(HeaderMenuHelper),
              new FrameworkPropertyMetadata(false, new PropertyChangedCallback(OnAllowPopupMenuCustomization)));
        public static void SetAllowPopupMenuCustomization(DependencyObject element, Boolean value)
        {
            element.SetValue(AllowPopupMenuCustomizationProperty, value);
        }
        public static Boolean GetAllowPopupMenuCustomization(DependencyObject element)
        {
            return (Boolean)element.GetValue(AllowPopupMenuCustomizationProperty);
        }
        #endregion AttachedProperties

        #region PivotPopupMenuCustomization
        public static void OnAllowPopupMenuCustomization(DependencyObject o, DependencyPropertyChangedEventArgs args)
        {
            PivotGridControl pivotGrid = o as PivotGridControl;
            if (pivotGrid == null) return;
            if( (Boolean)args.NewValue == true && (Boolean)args.OldValue == false )
                pivotGrid.PopupMenuShowing += pivotGrid_PopupMenuShowing;
            else if ( (Boolean)args.NewValue == false && (Boolean)args.OldValue == true )
                pivotGrid.PopupMenuShowing -= pivotGrid_PopupMenuShowing;

        }

        static void pivotGrid_PopupMenuShowing(object sender, PopupMenuShowingEventArgs e)
        {
            if (e.MenuType == PivotGridMenuType.Header)
            {
                PivotGridControl pivot = (PivotGridControl)sender;
                PivotGridField field = e.GetFieldInfo().Field;
                if (Convert.ToBoolean(field.GetValue(HeaderMenuHelper.AllowFieldSummaryTypeChangingProperty)))
                    e.Customizations.Add(CreateBarSubItem("Summary Type", "SummaryType", field));
                if (Convert.ToBoolean(field.GetValue(HeaderMenuHelper.AllowFieldSummaryDisplayTypeChangingProperty)))
                    e.Customizations.Add(CreateBarSubItem("Summary Display Type", "SummaryDisplayType", field));

            }
        }
        #endregion

        #region CustomBarItemsCreation

        private static BarSubItem CreateBarSubItem(string displayText, string propertyName, PivotGridField field)
        {
            BarSubItem barSubItem = new BarSubItem();
            barSubItem.Name = "bsi" + propertyName;
            barSubItem.Content = displayText;

            PropertyInfo property = typeof(PivotGridField).GetProperty(propertyName);

            foreach (object enumValue in Enum.GetValues(property.PropertyType))
            {
                BarCheckItem checkItem = new BarCheckItem();
                checkItem.Name = "bci" + propertyName + enumValue;
                checkItem.Content = enumValue.ToString();
                checkItem.IsChecked = Object.Equals(property.GetValue(field, new object[0]), enumValue);
                checkItem.Tag = new object[] { field, property, enumValue };
                checkItem.ItemClick += itemClickEventHandler;

                barSubItem.ItemLinks.Add(checkItem);
            }
            return barSubItem;
        }

        static void itemClickEventHandler(object sender, ItemClickEventArgs e)
        {
            BarItem barItem = sender as BarItem;
            object[] barItemInfo = (object[])barItem.Tag;
            PivotGridField field = (PivotGridField)barItemInfo[0];
            PropertyInfo property = (PropertyInfo)barItemInfo[1];
            object newValue = barItemInfo[2];
            property.SetValue(field, newValue, new object[0]);

        }
        #endregion CommonMethods
    }
}

The following code snippets (auto-collected from DevExpress Examples) contain references to the PopupMenuShowing event.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

See Also