Beispiel #1
0
        //
        //miSendto.Items.Add(miFilea);
        //MenuItem miOpen = new MenuItem();
        //miOpen.Header = "Open";
        //miOpen.Click += new RoutedEventHandler(this.menuitemFileOpen_Click);
        //miSendto.Items.Add(miOpen);
        //MenuItem miOpen1 = new MenuItem();
        //miOpen1.Header = "Recently Opened";
        //miOpen.Items.Add(miOpen1);
        //MenuItem miOpen1a = new MenuItem();
        //miOpen1a.Header = "Text.xaml";
        //miOpen1.Items.Add(miOpen1a);
        //contextmenu.Items.Add(miSendto);
        //datagrid.ContextMenu = contextmenu;
        #endregion

        private void DatagridContextmenuCreate()
        {
            var    comboboxitem = combobox.SelectedItem as ComboBoxItem;
            string selectedItem = comboboxitem.Content.ToString();

            Debug.Print(selectedItem);

            var context = new MyJukeboxEntities();
            var list    = DataGetSet.GetPlaylists();

            ContextMenu contextmenu = (ContextMenu)this.FindResource("contextmenuDatagrid");

            // add menu item to Mainmenu
            MenuItem miFile = new MenuItem();

            miFile.Header = "File";
            MenuItem miFileNew = new MenuItem();

            miFileNew.Header = "New";
            miFileNew.Click += new RoutedEventHandler(this.DatagridContextMenuitemFileNew_Click);
            miFile.Items.Add(miFileNew);

            contextmenu.Items.Add(miFile);

            // add menu items to existing menu
            MenuItem miSendto = (MenuItem)contextmenu.Items[0];

            miSendto.Items.Clear();

            if (selectedItem == "Audio")
            {
                miSendto.Header = "Send to";
                foreach (var playlist in list)
                {
                    MenuItem menuItem = new MenuItem();
                    menuItem.Header = playlist.Name;
                    menuItem.Tag    = playlist.ID;
                    menuItem.Click += new RoutedEventHandler(this.contextmenuDatagridSendtoPlaylist_Click);
                    miSendto.Items.Add(menuItem);
                }

                // remove menuitem 'remove'
                MenuItem mi = (MenuItem)contextmenu.Items[1];
                Debug.Print(mi.Header.ToString());
                contextmenu.Items.Remove(mi);

                MenuItem miRemove = new MenuItem();
                miRemove.Header = "Remove";
                miRemove.Click += new RoutedEventHandler(this.contextmenuDatagridRemoveFromAudio_Click);
                contextmenu.Items.Insert(1, miRemove);
            }

            if (selectedItem == "Playlist")
            {
                miSendto.Header = "Move to";
                foreach (var playlist in list)
                {
                    MenuItem menuItem = new MenuItem();
                    menuItem.Header = playlist.Name;
                    menuItem.Tag    = playlist.ID;
                    menuItem.Click += new RoutedEventHandler(this.contextmenuDatagridMovetoPlaylist_Click);
                    miSendto.Items.Add(menuItem);
                }

                // remove menuitem 'remove'
                MenuItem mi = (MenuItem)contextmenu.Items[1];
                Debug.Print(mi.Header.ToString());
                contextmenu.Items.Remove(mi);

                MenuItem miRemove = new MenuItem();
                miRemove.Header = "Remove";
                miRemove.Click += new RoutedEventHandler(this.contextmenuDatagridRemoveFromPlaylist_Click);
                contextmenu.Items.Insert(1, miRemove);
            }
        }