Example #1
0
        /// <summary>
        ///     Handles the XamContextMenu Opening event, verifies the element clicked is a CellControl
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void ContextMenuOpening(object sender, OpeningEventArgs e)
        {
            List <CellControl> elements = e.GetClickedElements <CellControl>();

            if (elements.Count == 0)
            {
                e.Cancel = true;
            }

            //Have to manually transfer menu args to my event args
            var args = new ContextMenuOpeningEventArgs {
                Cancel = e.Cancel, MouseClickLocation = e.MouseClickLocation
            };
            var firstOrDefault = elements.FirstOrDefault();

            if (firstOrDefault != null)
            {
                args.Cell = firstOrDefault.Cell;
            }
            args.Menu = _contextMenuMgr.ContextMenu;

            _contextMenuMgr.ContextMenu.DataContext = args.Cell.Row.Data;

            //Cast here because 'Grid' will return the base XamGrid type
            ((XamGridEx)Grid).OnContextMenuOpening(this, args);

            if (args.Cancel)
            {
                e.Cancel = true;
            }
        }
Example #2
0
        private void ContextMenu_Opening(object sender, OpeningEventArgs e)
        {
            var contextMenuManager = Infragistics.Controls.Menus.ContextMenuService.GetManager(AssociatedObject);
            var contextMenu        = contextMenuManager.ContextMenu;

            List <CellValuePresenter> rows       = e.GetClickedElements <CellValuePresenter>();
            List <LabelPresenter>     rowsHeader = e.GetClickedElements <LabelPresenter>();

            if (rowsHeader != null && rowsHeader.Any())
            {
                var row = rowsHeader.First();

                foreach (var item in headerMenuItems.OfType <XamMenuItem>())
                {
                    item.Header           = new Regex(@"([""'])(\\?.)*?\1").Replace(item.Header.ToString(), $"\"{row.Content.ToString()}\"");
                    item.CommandParameter = new HeaderCommandParameter
                    {
                        Grid      = AssociatedObject,
                        Column    = row.Content.ToString(),
                        ViewModel = ContextMenuHelper.GetViewModelName(AssociatedObject)
                    };
                }

                contextMenu.ItemsSource = headerMenuItems;
            }
            else if (rows != null && rows.Any())
            {
                contextMenu.ItemsSource = rowMenuItems;
            }
            else
            {
                var lst = headerMenuItems.Where(y => (string)y.Tag == ForceKey).ToList();

                if (rowMenuItems != null)
                {
                    if (lst.Any())
                    {
                        lst.Add(new XamMenuSeparator());
                    }

                    var items = rowMenuItems.Where(y => y.Tag != null && (string)y.Tag == ForceKey).ToList();

                    if (items.Any())
                    {
                        lst.AddRange(items);
                    }

                    if (lst.Any())
                    {
                        contextMenu.ItemsSource = lst;
                    }
                    else
                    {
                        contextMenu.IsOpen = false;
                    }
                }
                else
                {
                    contextMenu.IsOpen = false;
                }
            }
        }