void item_ItemClick(object sender, EventArgs e)
        {
            var item = sender as ContextMenuItem;

            if (item != null && item.HasSubMenu)
            {
                if (CurrentSubMenu != null)
                {
                    CurrentSubMenu.IsDropDownOpen = false;
                    CurrentSubMenu = null;
                }
                else
                {
                    ShowSubmenu(item);
                }
                return;
            }

            IsDropDownOpen = false;

            EventHandler handler = this.ItemClicked;

            if (handler != null)
            {
                handler(this, EventArgs.Empty);
            }
        }
 static void element_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
 {
     if ((Keyboard.Modifiers & ModifierKeys.Control) != ModifierKeys.None)
     {
         UIElement element = (UIElement)sender;
         var       getMenu = GetContextMenu(element);
         if (getMenu != null)
         {
             Point p    = e.GetPosition(null);
             var   menu = getMenu(p);
             HideCurrentMenu();
             if (menu != null)
             {
                 m_CurrentMenu = menu;
                 menu.SetLocation(p);
                 menu.IsDropDownOpen = true;
                 e.Handled           = true;
             }
         }
     }
 }
        protected virtual CustomContextMenu CreateContextMenu()
        {
            CustomContextMenu menu = new CustomContextMenu();
            m_ItemCut = new ContextMenuItem(Localization.ContextCmd_Cut);
            m_ItemCut.Icon = UriResources.Images.Cut16;
            m_ItemCut.ItemClick += new EventHandler(itemCut_ItemClick);
            menu.AddMenuItem(m_ItemCut);
            m_ItemCopy = new ContextMenuItem(Localization.ContextCmd_Copy);
            m_ItemCopy.Icon = UriResources.Images.Copy16;
            m_ItemCopy.ItemClick += new EventHandler(itemCopy_ItemClick);
            menu.AddMenuItem(m_ItemCopy);
            m_ItemPaste = new ContextMenuItem(Localization.ContextCmd_Paste);
            m_ItemPaste.Icon = UriResources.Images.Paste16;
            m_ItemPaste.ItemClick += new EventHandler(itemPaste_ItemClick);
            menu.AddMenuItem(m_ItemPaste);
            m_ItemDelete = new ContextMenuItem(Localization.ContextCmd_Delete);
            m_ItemDelete.Icon = UriResources.Images.Delete16;
            m_ItemDelete.ItemClick += new EventHandler(itemDelete_ItemClick);
            menu.AddMenuItem(m_ItemDelete);
            menu.AddMenuSplitter();
            ContextMenuItem item = new ContextMenuItem(Localization.ContextCmd_SelectAll);
            item.ItemClick += new EventHandler(itemSelectAll_ItemClick);
            menu.AddMenuItem(item);

            return menu;
        }
 public CustomContextMenuEventArgs(CustomContextMenu menu, PivotGridItem item)
 {
     Menu = menu;
     GridItem = item;
 }
        private CustomContextMenu CreateContextMenu(AreaType areaType)
        {
            CustomContextMenu contextMenu = new CustomContextMenu();
            contextMenu.Opened += new EventHandler(contextMenu_Opened);
            contextMenu.Closed += new EventHandler(contextMenu_Closed);
            ContextMenuItem item;

            if ((areaType == AreaType.RowsArea && Rows_IsInteractive) ||
                (areaType == AreaType.ColumnsArea && Columns_IsInteractive))
            {
                item = new ContextMenuItem(Localization.PivotGrid_Expand);
                item.Tag = MemberActionType.Expand;
                if (areaType == AreaType.RowsArea)
                {
                    item.Shortcut = Localization.PivotGrid_Rows_Expand_ShortCut;
                }
                else
                {
                    item.Shortcut = Localization.PivotGrid_Columns_Expand_ShortCut;
                }
                contextMenu.AddMenuItem(item);
                item.ItemClick += new EventHandler(ContextMenu_ItemClick);

                item = new ContextMenuItem(Localization.PivotGrid_Collapse);
                item.Tag = MemberActionType.Collapse;
                if (areaType == AreaType.RowsArea)
                {
                    item.Shortcut = Localization.PivotGrid_Rows_Collapse_ShortCut;
                }
                else
                {
                    item.Shortcut = Localization.PivotGrid_Columns_Collapse_ShortCut;
                }
                contextMenu.AddMenuItem(item);
                item.ItemClick += new EventHandler(ContextMenu_ItemClick);

                item = new ContextMenuItem(Localization.PivotGrid_DrillDown);
                item.Tag = MemberActionType.DrillDown;
                if (areaType == AreaType.RowsArea)
                {
                    item.Shortcut = Localization.PivotGrid_Rows_Drilldown_ShortCut;
                }
                else
                {
                    item.Shortcut = Localization.PivotGrid_Columns_Drilldown_ShortCut;
                }
                contextMenu.AddMenuItem(item);
                item.ItemClick += new EventHandler(ContextMenu_ItemClick);

                contextMenu.AddMenuSplitter();
            }

            // Пуекты для сортировки элементов
            if ((areaType == AreaType.RowsArea) ||
                (areaType == AreaType.ColumnsArea))
            {
                item = new ContextMenuItem(Localization.ContextMenu_SortingByProperty);
                item.Tag = ControlActionType.SortingByProperty;
                contextMenu.AddMenuItem(item);
                item.ItemClick += new EventHandler(ContextMenu_ItemClick);

                item = new ContextMenuItem(Localization.ContextMenu_SortingByMeasure);
                item.Tag = ControlActionType.SortingAxisByMeasure;
                contextMenu.AddMenuItem(item);
                item.ItemClick += new EventHandler(ContextMenu_ItemClick);

                item = new ContextMenuItem(Localization.ContextMenu_SortingByValue);
                item.Tag = ControlActionType.SortingByValue;
                contextMenu.AddMenuItem(item);
                item.ItemClick += new EventHandler(ContextMenu_ItemClick);

                //item = new ContextMenuItem(Localization.ContextMenu_ClearAxisSorting);
                //item.Tag = ControlActionType.ClearAxisSorting;
                //contextMenu.AddMenuItem(item);
                //item.ItemClick += new EventHandler(ContextMenu_ItemClick);

                contextMenu.AddMenuSplitter();
            }

            if (areaType == AreaType.CellsArea)
            {

                m_CopyValueMenuItem = new ContextMenuItem(Localization.ContextMenu_CopyValue + "      ");
                m_CopyValueMenuItem.Tag = ControlActionType.ValueCopy;
                //item.Icon = UriResources.Images.ActionNode16;
                contextMenu.AddMenuItem(m_CopyValueMenuItem);
                m_CopyValueMenuItem.ItemClick += new EventHandler(ContextMenu_ItemClick);

                m_DeliveryValueMenuItem = new ContextMenuItem(Localization.ContextMenu_DeliveryValue + "      ");
                m_DeliveryValueMenuItem.Tag = ControlActionType.ValueDelivery;
                //item.Icon = UriResources.Images.ActionNode16;
                contextMenu.AddMenuItem(m_DeliveryValueMenuItem);
                m_DeliveryValueMenuItem.ItemClick += new EventHandler(ContextMenu_ItemClick);

                m_DeliveryValueSplitter = contextMenu.AddMenuSplitter();

                m_DrillThroughMenuItem = new ContextMenuItem(Localization.ContextMenu_DrillThrough);
                m_DrillThroughMenuItem.Tag = ControlActionType.DrillThrough;
                //item.Icon = UriResources.Images.ActionNode16;
                contextMenu.AddMenuItem(m_DrillThroughMenuItem);
                m_DrillThroughMenuItem.ItemClick += new EventHandler(ContextMenu_ItemClick);

                m_CopySelectedCellsMenuItem = new ContextMenuItem(Localization.ContextMenu_Copy);
                m_CopySelectedCellsMenuItem.Tag = ControlActionType.Copy;
                m_CopySelectedCellsMenuItem.Icon = UriResources.Images.Copy16;
                contextMenu.AddMenuItem(m_CopySelectedCellsMenuItem);
                m_CopySelectedCellsMenuItem.ItemClick += new EventHandler(ContextMenu_ItemClick);

                m_PasteSelectedCellsMenuItem = new ContextMenuItem(Localization.ContextMenu_Paste);
                m_PasteSelectedCellsMenuItem.Tag = ControlActionType.Paste;
                m_PasteSelectedCellsMenuItem.Icon = UriResources.Images.Paste16;
                contextMenu.AddMenuItem(m_PasteSelectedCellsMenuItem);
                m_PasteSelectedCellsMenuItem.ItemClick += new EventHandler(ContextMenu_ItemClick);

                m_CopyCellsSplitter = contextMenu.AddMenuSplitter();

                if (!CanEdit)
                {
                    m_PasteSelectedCellsMenuItem.Visibility = Visibility.Collapsed;
                    m_CopyValueMenuItem.Visibility = Visibility.Collapsed;
                    m_DeliveryValueMenuItem.Visibility = Visibility.Collapsed;
                    m_DeliveryValueSplitter.Visibility = Visibility.Collapsed;
                }
                else
                {
                    m_PasteSelectedCellsMenuItem.IsEnabled = EditMode & CanEdit;
                }

                if (!DrillThroughCells)
                    m_DrillThroughMenuItem.Visibility = Visibility.Collapsed;
            }

            item = new CheckedContectMenuItem(Localization.ContextMenu_AutoWidthColumns);
            item.Tag = ControlActionType.AutoWidth;
            (item as CheckedContectMenuItem).IsChecked = AutoWidthColumns;
            contextMenu.AddMenuItem(item);
            item.ItemClick += new EventHandler(ContextMenu_ItemClick);

            #region Подменю для управления режимом реорганизации данных
            CustomContextMenu subMenu1 = new CustomContextMenu();
            item = new CheckedContectMenuItem(Localization.ContextMenu_DataReorganizationType_None);
            item.Tag = ControlActionType.DataReorganizationType_None;
            subMenu1.AddMenuItem(item);
            item.ItemClick += new EventHandler(ContextMenu_ItemClick);

            item = new CheckedContectMenuItem(Localization.ContextMenu_DataReorganizationType_MergeNeighbors);
            item.Tag = ControlActionType.DataReorganizationType_MergeNeighbors;
            subMenu1.AddMenuItem(item);
            item.ItemClick += new EventHandler(ContextMenu_ItemClick);

            item = new CheckedContectMenuItem(Localization.ContextMenu_DataReorganizationType_HitchToParent);
            item.Tag = ControlActionType.DataReorganizationType_HitchToParent;
            subMenu1.AddMenuItem(item);
            item.ItemClick += new EventHandler(ContextMenu_ItemClick);

            item = new ContextMenuItem(Localization.ContextMenu_DataReorganizationType);
            item.Tag = ControlActionType.DataReorganizationType;
            item.SubMenu = subMenu1;
            contextMenu.AddMenuItem(item);
            #endregion

            item = new ContextMenuItem(Localization.ContextMenu_ShowMDX);
            item.Tag = ControlActionType.ShowMDX;
            item.Icon = UriResources.Images.Mdx16;
            contextMenu.AddMenuItem(item);
            item.ItemClick += new EventHandler(ContextMenu_ItemClick);

            if (areaType == AreaType.RowsArea || areaType == AreaType.ColumnsArea)
            {
                item = new ContextMenuItem(Localization.ContextMenu_CustomProperties);
                item.Tag = ControlActionType.ShowAttributes;
                item.Icon = UriResources.Images.MemberProperty16;
                contextMenu.AddMenuItem(item);
                item.ItemClick += new EventHandler(ContextMenu_ItemClick);
            }

            item = new ContextMenuItem(Localization.ContextMenu_Properties);
            item.Tag = ControlActionType.ShowProperties;
            item.Icon = UriResources.Images.LevelProperty16;
            contextMenu.AddMenuItem(item);
            item.ItemClick += new EventHandler(ContextMenu_ItemClick);

            return contextMenu;
        }
        CustomContextMenu GetCurrentContextMenu(Point p)
        {
            m_ContextMenu = null; 
            PivotGridItem grid_item = null;

            if (AgControlBase.GetSLBounds(ItemsLayoutRoot).Contains(p))
            {
                grid_item = PivotGridItem.GetPivotGridItem(p);
                MemberControl member_control = grid_item as MemberControl;
                if (member_control != null)
                {
                    if (grid_item is RowMemberControl)
                    {
                        m_ContextMenu = Rows_ContextMenu;
                    }
                    if (grid_item is ColumnMemberControl)
                    {
                        m_ContextMenu = Columns_ContextMenu;
                    }

                    if (m_ContextMenu != null)
                    {
                        // Делаем доступными пункты меню только если это необходимо
                        foreach (UIElement element in m_ContextMenu.Items)
                        {
                            ContextMenuItem menu_item = element as ContextMenuItem;
                            if (menu_item != null)
                            {
                                if (menu_item.Tag is MemberActionType)
                                {
                                    // Если пункт меню является в принципе доступным, то устанавливаем его доступность с учетом текущего состояния
                                    if (((MemberActionType)menu_item.Tag == MemberActionType.Collapse) || ((MemberActionType)menu_item.Tag == MemberActionType.Expand))
                                    {
                                        if ((MemberActionType)menu_item.Tag == MemberActionType.Collapse)
                                        {
                                            // Учитываем доступность впринципе и текущее состояние элемента
                                            menu_item.IsEnabled = member_control.UseExpandingCommands & member_control.Member.DrilledDown;
                                        }
                                        if ((MemberActionType)menu_item.Tag == MemberActionType.Expand)
                                        {
                                            // Учитываем доступность впринципе и текущее состояние элемента
                                            menu_item.IsEnabled = member_control.UseExpandingCommands & !member_control.Member.DrilledDown;
                                        }
                                    }
                                }

                                if (menu_item.Tag is ControlActionType)
                                {
                                    ControlActionType action = (ControlActionType)menu_item.Tag;
                                    switch (action)
                                    {
                                        case ControlActionType.SortingByValue:
                                            // Доступность пункта меню - Сортировка по значению
                                            // Делаем доступным только для элементов последних линий
                                            menu_item.IsEnabled = member_control.Member != null && member_control.Member.Children.Count == 0;
                                            break;
                                    }
                                }
                            }
                        }
                    }
                }

                if (grid_item is CellControl || grid_item == null)
                {
                    m_ContextMenu = Cells_ContextMenu;

                    CellControl cell_Control = grid_item as CellControl;
                    if (cell_Control != null)
                    {
                        // Устанавливаем фокус на данную ячеку если она не находится в списке выбранных
                        if (!Selection.Contains(cell_Control.Cell))
                            FocusedCellView = cell_Control.Cell;
                    }

                    // Если меню вызвано за пределами области ячеек, то будет относиться к ячейке с фокусом
                    CellInfo info = cell_Control != null ? cell_Control.Cell : FocusedCellView;

                    if (m_DeliveryValueMenuItem != null)
                        m_DeliveryValueMenuItem.IsEnabled = info != null && info.IsUpdateable;
                    if (m_CopyValueMenuItem != null)
                        m_CopyValueMenuItem.IsEnabled = info != null && info.IsUpdateable;

                    if (m_PasteSelectedCellsMenuItem != null)
                        m_PasteSelectedCellsMenuItem.IsEnabled = EditMode & CanEdit;
                    IList<CellInfo> cells = Selection;
                    if (m_CopySelectedCellsMenuItem != null)
                        m_CopySelectedCellsMenuItem.IsEnabled = cells != null && cells.Count > 0;
                    if (m_DrillThroughMenuItem != null)
                        m_DrillThroughMenuItem.IsEnabled = info != null && !info.IsCalculated && cells != null && cells.Count == 1;
                }

                if (m_ContextMenu != null)
                {
                    m_ContextMenu.Tag = grid_item;
                }

                if (m_ContextMenu != null)
                {
                    foreach (UIElement element in m_ContextMenu.Items)
                    {
                        CheckedContectMenuItem menu_item = element as CheckedContectMenuItem;
                        if (menu_item != null && menu_item.Tag is ControlActionType)
                        {
                            if ((ControlActionType)(menu_item.Tag) == ControlActionType.AutoWidth)
                            {
                                menu_item.IsChecked = AutoWidthColumns;
                            }
                        }
                    }
                }
            }

            var handler = InitializeContextMenu;
            CustomContextMenuEventArgs args = new CustomContextMenuEventArgs(m_ContextMenu, grid_item);
            if (handler != null)
            {
                handler(this, args);
            }

            return args.Menu;
        }
        protected virtual CustomContextMenu CreateContextMenu()
        {
            CustomContextMenu contextMenu = new CustomContextMenu();
            ContextMenuItem item;

            m_MoveUp_MenuItem = new ContextMenuItem(Localization.MdxDesigner_ContextMenu_MoveUp);
            m_MoveUp_MenuItem.Icon = UriResources.Images.Up16;
            contextMenu.AddMenuItem(m_MoveUp_MenuItem);
            m_MoveUp_MenuItem.ItemClick += new EventHandler(MoveUp_ItemClick);

            m_MoveDown_MenuItem = new ContextMenuItem(Localization.MdxDesigner_ContextMenu_MoveDown);
            m_MoveDown_MenuItem.Icon = UriResources.Images.Down16;
            contextMenu.AddMenuItem(m_MoveDown_MenuItem);
            m_MoveDown_MenuItem.ItemClick += new EventHandler(MoveDown_ItemClick);

            //contextMenu.AddMenuSplitter();

            item = new ContextMenuItem(Localization.MdxDesigner_ContextMenu_RemoveField);
            item.Icon = UriResources.Images.RemoveHot16;
            contextMenu.AddMenuItem(item);
            item.ItemClick += new EventHandler(Remove_ItemClick);

            // Используем открытие и закрытие меню для работы с тултипом
            contextMenu.PopupControl.Closed -= new EventHandler(PopupControl_Closed);
            contextMenu.PopupControl.Closed += new EventHandler(PopupControl_Closed);
            contextMenu.PopupControl.Opened -= new EventHandler(PopupControl_Opened);
            contextMenu.PopupControl.Opened += new EventHandler(PopupControl_Opened);
            return contextMenu;
        }
		protected virtual CustomContextMenu CreateContextMenu()
		{
			CustomContextMenu contextMenu = new CustomContextMenu();
			return contextMenu;
		}
        private CustomContextMenu CreateContextMenu(AreaType areaType)
        {
            CustomContextMenu contextMenu = new CustomContextMenu();
            contextMenu.Opened += new EventHandler(contextMenu_Opened);
            contextMenu.Closed += new EventHandler(contextMenu_Closed);
            ContextMenuItem item;

            if ((areaType == AreaType.RowsArea && Rows_IsInteractive) ||
                (areaType == AreaType.ColumnsArea && Columns_IsInteractive))
            {
                item = new ContextMenuItem(Localization.PivotGrid_Expand);
                item.Tag = MemberActionType.Expand;
                if (areaType == AreaType.RowsArea)
                {
                    item.Shortcut = Localization.PivotGrid_Rows_Expand_ShortCut;
                }
                else
                {
                    item.Shortcut = Localization.PivotGrid_Columns_Expand_ShortCut;
                }
                contextMenu.AddMenuItem(item);
                item.ItemClick += new EventHandler(ContextMenu_ItemClick);

                item = new ContextMenuItem(Localization.PivotGrid_Collapse);
                item.Tag = MemberActionType.Collapse;
                if (areaType == AreaType.RowsArea)
                {
                    item.Shortcut = Localization.PivotGrid_Rows_Collapse_ShortCut;
                }
                else
                {
                    item.Shortcut = Localization.PivotGrid_Columns_Collapse_ShortCut;
                }
                contextMenu.AddMenuItem(item);
                item.ItemClick += new EventHandler(ContextMenu_ItemClick);

                item = new ContextMenuItem(Localization.PivotGrid_DrillDown);
                item.Tag = MemberActionType.DrillDown;
                if (areaType == AreaType.RowsArea)
                {
                    item.Shortcut = Localization.PivotGrid_Rows_Drilldown_ShortCut;
                }
                else
                {
                    item.Shortcut = Localization.PivotGrid_Columns_Drilldown_ShortCut;
                }
                contextMenu.AddMenuItem(item);
                item.ItemClick += new EventHandler(ContextMenu_ItemClick);

                contextMenu.AddMenuSplitter();
            }

            if (areaType == AreaType.CellsArea)
            {

                m_CopyValueMenuItem = new ContextMenuItem(Localization.ContextMenu_CopyValue + "      ");
                m_CopyValueMenuItem.Tag = ControlActionType.ValueCopy;
                //item.Icon = UriResources.Images.ActionNode16;
                contextMenu.AddMenuItem(m_CopyValueMenuItem);
                m_CopyValueMenuItem.ItemClick += new EventHandler(ContextMenu_ItemClick);

                m_DeliveryValueMenuItem = new ContextMenuItem(Localization.ContextMenu_DeliveryValue + "      ");
                m_DeliveryValueMenuItem.Tag = ControlActionType.ValueDelivery;
                //item.Icon = UriResources.Images.ActionNode16;
                contextMenu.AddMenuItem(m_DeliveryValueMenuItem);
                m_DeliveryValueMenuItem.ItemClick += new EventHandler(ContextMenu_ItemClick);

                m_DeliveryValueSplitter = contextMenu.AddMenuSplitter();

                m_DrillThroughMenuItem = new ContextMenuItem(Localization.ContextMenu_DrillThrough);
                m_DrillThroughMenuItem.Tag = ControlActionType.DrillThrough;
                //item.Icon = UriResources.Images.ActionNode16;
                contextMenu.AddMenuItem(m_DrillThroughMenuItem);
                m_DrillThroughMenuItem.ItemClick += new EventHandler(ContextMenu_ItemClick);

                m_CopySelectedCellsMenuItem = new ContextMenuItem(Localization.ContextMenu_Copy);
                m_CopySelectedCellsMenuItem.Tag = ControlActionType.Copy;
                m_CopySelectedCellsMenuItem.Icon = UriResources.Images.Copy16;
                contextMenu.AddMenuItem(m_CopySelectedCellsMenuItem);
                m_CopySelectedCellsMenuItem.ItemClick += new EventHandler(ContextMenu_ItemClick);

                m_PasteSelectedCellsMenuItem = new ContextMenuItem(Localization.ContextMenu_Paste);
                m_PasteSelectedCellsMenuItem.Tag = ControlActionType.Paste;
                m_PasteSelectedCellsMenuItem.Icon = UriResources.Images.Paste16;
                contextMenu.AddMenuItem(m_PasteSelectedCellsMenuItem);
                m_PasteSelectedCellsMenuItem.ItemClick += new EventHandler(ContextMenu_ItemClick);

                m_CopyCellsSplitter = contextMenu.AddMenuSplitter();

                if (!CanEdit)
                {
                    m_PasteSelectedCellsMenuItem.Visibility = Visibility.Collapsed;
                    m_CopyValueMenuItem.Visibility = Visibility.Collapsed;
                    m_DeliveryValueMenuItem.Visibility = Visibility.Collapsed;
                    m_DeliveryValueSplitter.Visibility = Visibility.Collapsed;
                }
                else
                {
                    m_PasteSelectedCellsMenuItem.IsEnabled = EditMode & CanEdit;
                }

                if (!DrillThroughCells)
                    m_DrillThroughMenuItem.Visibility = Visibility.Collapsed;
            }

            item = new ContextMenuItem(Localization.ContextMenu_ShowMDX);
            item.Tag = ControlActionType.ShowMDX;
            item.Icon = UriResources.Images.Mdx16;
            contextMenu.AddMenuItem(item);
            item.ItemClick += new EventHandler(ContextMenu_ItemClick);

            if (areaType == AreaType.RowsArea || areaType == AreaType.ColumnsArea)
            {
                item = new ContextMenuItem(Localization.ContextMenu_CustomProperties);
                item.Tag = ControlActionType.ShowAttributes;
                item.Icon = UriResources.Images.MemberProperty16;
                contextMenu.AddMenuItem(item);
                item.ItemClick += new EventHandler(ContextMenu_ItemClick);
            }

            item = new ContextMenuItem(Localization.ContextMenu_Properties);
            item.Tag = ControlActionType.ShowProperties;
            item.Icon = UriResources.Images.LevelProperty16;
            contextMenu.AddMenuItem(item);
            item.ItemClick += new EventHandler(ContextMenu_ItemClick);

            return contextMenu;
        }
        CustomContextMenu GetCurrentContextMenu(Point p)
        {
            m_ContextMenu = null; 
            
            if (AgControlBase.GetSLBounds(ItemsLayoutRoot).Contains(p))
            {
                PivotGridItem grid_item = PivotGridItem.GetPivotGridItem(p);
                if (grid_item != null)
                {
                    if (grid_item is RowMemberControl)
                    {
                        m_ContextMenu = Rows_ContextMenu;
                    }
                    if (grid_item is ColumnMemberControl)
                    {
                        m_ContextMenu = Columns_ContextMenu;
                    }

                    MemberControl member_control = grid_item as MemberControl;
                    if (member_control != null && m_ContextMenu != null)
                    {
                        // Делаем доступными пункты меню только если это необходимо
                        foreach (UIElement element in m_ContextMenu.Items)
                        {
                            ContextMenuItem menu_item = element as ContextMenuItem;
                            if (menu_item != null)
                            {
                                if (menu_item.Tag is MemberActionType)
                                {
                                    // Если пункт меню является в принципе доступным, то устанавливаем его доступность с учетом текущего состояния
                                    if (((MemberActionType)menu_item.Tag == MemberActionType.Collapse) || ((MemberActionType)menu_item.Tag == MemberActionType.Expand))
                                    {
                                        if ((MemberActionType)menu_item.Tag == MemberActionType.Collapse)
                                        {
                                            // Учитываем доступность впринципе и текущее состояние элемента
                                            menu_item.IsEnabled = member_control.UseExpandingCommands & member_control.Member.DrilledDown;
                                        }
                                        if ((MemberActionType)menu_item.Tag == MemberActionType.Expand)
                                        {
                                            // Учитываем доступность впринципе и текущее состояние элемента
                                            menu_item.IsEnabled = member_control.UseExpandingCommands & !member_control.Member.DrilledDown;
                                        }
                                    }
                                }
                            }
                        }
                    }

                    if (grid_item is CellControl)
                    {
                        m_ContextMenu = Cells_ContextMenu;

                        CellControl cell_Control = grid_item as CellControl;
                        if (cell_Control != null && cell_Control.Cell != null && cell_Control.Cell.IsUpdateable)
                        {
                            if (m_DeliveryValueMenuItem != null)
                                m_DeliveryValueMenuItem.IsEnabled = true;
                            if (m_CopyValueMenuItem != null)
                                m_CopyValueMenuItem.IsEnabled = true;
                        }
                        else
                        {
                            if (m_DeliveryValueMenuItem != null)
                                m_DeliveryValueMenuItem.IsEnabled = false;
                            if (m_CopyValueMenuItem != null)
                                m_CopyValueMenuItem.IsEnabled = false;
                        }

                        m_DrillThroughMenuItem.IsEnabled = !(cell_Control.Cell != null && cell_Control.Cell.IsCalculated);
                        m_PasteSelectedCellsMenuItem.IsEnabled = EditMode & CanEdit;
                    }

                    if (m_ContextMenu != null)
                    {
                        m_ContextMenu.Tag = grid_item;
                    }
                }

                //var args = new MemberClickEventArgs(this.Member, p);
                //Raise_ShowContextMenu(args);

                //return args.ContextMenu;
            }

            return m_ContextMenu;
        }
 static void element_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
 {
     if ((Keyboard.Modifiers & ModifierKeys.Control) != ModifierKeys.None)
     {
         UIElement element = (UIElement)sender;
         var getMenu = GetContextMenu(element);
         if (getMenu != null)
         {
             Point p = e.GetPosition(null);
             var menu = getMenu(p);
             HideCurrentMenu();
             if (menu != null)
             {
                 m_CurrentMenu = menu;
                 menu.SetLocation(p);
                 menu.IsDropDownOpen = true;
                 e.Handled = true;
             }
         }
     }
 }
        void item_ItemClick(object sender, EventArgs e)
        {
            var item = sender as ContextMenuItem;
            if (item != null && item.HasSubMenu)
            {
                if (CurrentSubMenu != null)
                {
                    CurrentSubMenu.IsDropDownOpen = false;
                    CurrentSubMenu = null;
                }
                else
                {
                    ShowSubmenu(item);
                }
                return;
            }

            IsDropDownOpen = false;

            EventHandler handler = this.ItemClicked;
            if (handler != null)
            {
                handler(this, EventArgs.Empty);
            }
        }