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;
        }
		void PivotGrid_InitializeContextMenu(object sender, CustomContextMenuEventArgs e)
		{
			if (e != null && e.GridItem != null && e.Menu != null)
			{
				MemberControl member_control = e.GridItem as MemberControl;
				if (member_control != null)
				{
					foreach (UIElement element in e.Menu.Items)
					{
						ContextMenuItem menu_item = element as ContextMenuItem;
						if (menu_item != null)
						{
							if (menu_item.Tag is ControlActionType)
							{
								ControlActionType action = (ControlActionType)menu_item.Tag;
								switch (action)
								{
									case ControlActionType.SortingByProperty:
										// Иконка для пункта меню - Сортировка по свойству
										var property_sort = GetAxisPropertySort(member_control);
										switch (property_sort != null ? property_sort.Type : SortTypes.None)
										{
											case SortTypes.Ascending:
												menu_item.Icon = UriResources.Images.SortAZ16;
												break;
											case SortTypes.Descending:
												menu_item.Icon = UriResources.Images.SortZA16;
												break;
											default:
												menu_item.Icon = null;
												break;
										}
										break;
									case ControlActionType.SortingAxisByMeasure:
										// Иконка для пункта меню - Сортировка по значению
										var measure_sort = GetAxisByMeasureSort(member_control) as SortByMeasureDescriptor;
										switch (measure_sort != null ? measure_sort.Type : SortTypes.None)
										{
											case SortTypes.Ascending:
												menu_item.Icon = UriResources.Images.SortAZ16;
												break;
											case SortTypes.Descending:
												menu_item.Icon = UriResources.Images.SortZA16;
												break;
											default:
												menu_item.Icon = null;
												break;
										}
										break;
									case ControlActionType.SortingByValue:
										menu_item.Icon = null;
										if (member_control is ColumnMemberControl || member_control is RowMemberControl)
										{
											var value_sort = GetAxisSort(member_control is ColumnMemberControl ? AreaType.RowsArea : AreaType.ColumnsArea) as SortByValueDescriptor;
											if (value_sort != null && member_control.Member != null && value_sort.CompareByTuple(member_control.Member.GetAxisTuple()))
											{
												switch (value_sort.Type)
												{
													case SortTypes.Ascending:
														menu_item.Icon = UriResources.Images.SortAZ16;
														break;
													case SortTypes.Descending:
														menu_item.Icon = UriResources.Images.SortZA16;
														break;
													default:
														menu_item.Icon = null;
														break;
												}
											}
										}
										break;
									case ControlActionType.ClearAxisSorting:
										// Доступность пункта меню - Удалить сортировку
										var property_sort1 = GetAxisPropertySort(member_control);
										var measure_sort1 = GetAxisByMeasureSort(member_control);
										SortTypes property_sort_type = property_sort1 != null ? property_sort1.Type : SortTypes.None;
										SortTypes measure_sort_type = measure_sort1 != null ? measure_sort1.Type : SortTypes.None;

										menu_item.IsEnabled = property_sort_type != SortTypes.None || measure_sort_type != SortTypes.None;
										break;
								}
							}
						}
					}
				}
			}

			if (e.Menu != null)
			{
				foreach (UIElement element in e.Menu.Items)
				{
					var menu_item = element as ContextMenuItem;
					if (menu_item != null && menu_item.Tag is ControlActionType)
					{
						switch ((ControlActionType)(menu_item.Tag))
						{
							case ControlActionType.DataReorganizationType:
								if (menu_item.SubMenu != null)
								{
									foreach (UIElement child in menu_item.SubMenu.Items)
									{
										var checked_item = child as CheckedContectMenuItem;
										if (checked_item != null && checked_item.Tag is ControlActionType)
										{
											switch ((ControlActionType)(checked_item.Tag))
											{
												case ControlActionType.DataReorganizationType_None:
													checked_item.IsChecked = DataReorganizationType == DataReorganizationTypes.None;
													break;
												case ControlActionType.DataReorganizationType_HitchToParent:
													checked_item.IsChecked = DataReorganizationType == DataReorganizationTypes.LinkToParent;
													break;
												case ControlActionType.DataReorganizationType_MergeNeighbors:
													checked_item.IsChecked = DataReorganizationType == DataReorganizationTypes.MergeNeighbors;
													break;
											}
										}
									}
								}
								break;
						}
					}
				}
			}
		}