Generate a PopMenu programmatically

Jul 13, 2011 at 6:45 AM
Edited Jul 13, 2011 at 6:58 AM


Hi,

yesterday I wrote with ZiadJ about the topic "How to generate a PopMenu programmatically", here is my solution as a quick draft. I hope it will help somebody in the same situation.

View.XAML

<controls:CustomDataGrid x:Name="datagrid" 
                                     ItemsSource="{Binding PCV}" >
<sdk:DataGrid.Columns>

!!!ColumnDefintion!!!

</sdk:DataGrid.Columns>

</controls:CustomDataGrid>

 

View.XAML.cs

 public View()
{
            InitializeComponent();
            this.DataContext = new ViewModel();

            ViewModel rvm = (ViewModel)DataContext;
            rvm.InternalUIElement = datagrid;
            //I know I put UI Elements to the VM....
}
ViewModel.cs
//The complete Commands are not listed, it's just the dynamically PopMenu generation for a DataGrid. I hope it will help nevertheless
 //My complete PopMenu supports Search and Replace over the DataGrid and into the DataBase, Copy to Excel and Paste from Excel to the DataGrid/DataBase, Grouping mechanism, and insert, update, delete support 

private UIElement pInternalUIElement;
public UIElement InternalUIElement
{
   get
   {
       return pInternalUIElement;
   }
   set
   {
       pInternalUIElement = value;
       DataGridMenuGenerator();
   }
}

 private void DataGridMenuGenerator()
        {
            //Create the menu
            var pmMain = new PopupMenu();
            
            if (pInternalUIElement != null)
            {

                pmMain.AddTrigger(TriggerTypes.RightClick, InternalUIElement);

                pmMain.Opening += delegate
                {
                    #region DataGridColumnHeader Menu

                    DataGridColumnHeader columnHeader = pmMain.GetClickedElement();
                    if (columnHeader != null)
                    {
                        //ColumnHeader mit Informationen
                        if (columnHeader.Content != null)
                        {
                            pmMain.AddItem("/SL;component/Images/Group.png", String.Format("Group  {0}", columnHeader.Content), null, delegate
                            {
                                _GroupCommand(columnHeader);
                            });

                            pmMain.AddSeparator();

                            pmMain.AddItem("/SL;component/Images/SearchAndReplace.png", "Search and Replace", null, delegate
                            {
                                _SearchAndReplaceCommand(new SearchAndReplaceCustomModel(columnList, columnHeader.Content.ToString(), null, null));
                            });
                        }
                        else
                        {
                            pmMain.AddItem("/SL;component/Images/Copy.png", "Copy hole Grid to Clipboard", null, delegate
                            {
                                _CopyAllItemsToClipBoardCommand(null);
                            });

                            pmMain.AddSeparator();

                            pmMain.AddItem("/SL;component/Images/Paste.png", "Paste from Clipboard", null, delegate
                            {
                                _PasteDataFromClipBoardCommand(null);
                            });
                        }
                    }

                    #endregion

                    #region DataGridRowGroupHeader Menu

                    DataGridRowGroupHeader rowGroupHeader = pmMain.GetClickedElement();
                    if (rowGroupHeader != null)
                    {
                        pmMain.AddItem("/SL;component/Images/UnGroup.png", "UnGroup", null, delegate
                        {
                            _UnGroupCommand(rowGroupHeader.PropertyName);
                        });
                    }

                    #endregion

                    #region DataGridCell / DataGridRow Menu

                    DataGridCell cell = pmMain.GetClickedElement();
                    if (cell != null)
                    {
                        DependencyObject dep = cell;
                        Object element = ((ContentControl)(cell)).Content;
                        string visualElementString = String.Empty;
                        if (element is TextBlock)
                        {
                            visualElementString = ((TextBlock)(element)).Text;
                        }
                        else if (element is CheckBox)
                        {
                            visualElementString = ((CheckBox)(element)).IsChecked.ToString();
                        }

                        // try to get Row
                        while ((dep != null) && !(dep is DataGridRow))
                        {
                            dep = VisualTreeHelper.GetParent(dep);
                        }

                        if (dep == null)
                            return;

                        DataGridRow row = dep as DataGridRow;

                        //Try to get Column
                        DataGridColumn column = DataGridColumn.GetColumnContainingElement(cell);

                        Role role = (Role)((System.Windows.FrameworkElement)(row)).DataContext;
                        PCV.MoveCurrentTo(role);

                        pmMain.AddItem("/SL;component/Images/Insert02.png", "Insert Role", null, delegate
                        {
                            _OpenChildWindowCommand(new DataGridCustomModel { _enumDataGridAction = enumDataGridAction.insert, ObjectModel = null });
                        });

                        pmMain.AddItem("/SL;component/Images/Update.png", String.Format("Update {0}", role.Name_Role), null, delegate
                        {
                            _OpenChildWindowCommand(new DataGridCustomModel { _enumDataGridAction = enumDataGridAction.update, ObjectModel = role });
                        });

                        pmMain.AddItem("/SL;component/Images/sub.png", String.Format("delete {0}", role.Name_Role), null, delegate
                        {
                            _OpenChildWindowCommand(new DataGridCustomModel { _enumDataGridAction = enumDataGridAction.delete, ObjectModel = role });
                        });

                        pmMain.AddSeparator();

                        pmMain.AddItem("/SL;component/Images/SearchAndReplace.png", String.Format("Search and Replace in Column {0} with the Value {1}", column.Header, visualElementString), null, delegate
                        {
                            _SearchAndReplaceCommand(new SearchAndReplaceCustomModel(columnList, column.Header.ToString(), visualElementString, null));
                        });
                    }

                    #endregion

                    #region DataGridRowHeader Menu

                    DataGridRowHeader datagridRowHeader = pmMain.GetClickedElement();

                    if (datagridRowHeader != null)
                    {
                        DependencyObject dep = datagridRowHeader;

                        // try to get Row
                        while ((dep != null) && !(dep is DataGridRow))
                        {
                            dep = VisualTreeHelper.GetParent(dep);
                        }

                        if (dep == null)
                            return;

                        DataGridRow row = dep as DataGridRow;
                        Role role = (Role)((System.Windows.FrameworkElement)(row)).DataContext;

                        pmMain.AddItem("/SL;component/Images/Copy.png", "Copy to ClipBoard", null, delegate
                        {
                            _CopySelectedItemsToClipBoardCommand(null);
                        });

                        pmMain.AddItem("/SL;component/Images/Paste.png", "Paste from ClipBoard", null, delegate
                        {
                            _PasteDataFromClipBoardCommand(null);
                        });

                        pmMain.AddSeparator();

                        pmMain.AddItem("/SL;component/Images/Update.png", String.Format("Update \"{0}\" from ClipBoard", role.TXT_Description_Short), null, delegate
                        {
                            _UpdateDataFromClipBoardCommand(role);
                        });
                    }

                    #endregion

                };
                pmMain.Closing += delegate { pmMain.Items.Clear(); };
            }
        }

Kind regards
Daniel