A feature that implements the right-click menu Close option on the ListBoxItem control in the ListView to remove the ListBoxItem from the ListView.
Use the RoutedCommand class to create command commands, which are defined on MSDN as an implementation ICommand and routed within the element tree.
C # code:
PrivateRoutedCommand Closecmd =NewRoutedCommand ("Clear",typeof(MainWindow));Private voidListboxitem_mouserightbuttonup (ObjectSender,mousebuttoneventargs e) {ListBoxItem Data=NewListBoxItem (); Data=(ListBoxItem) sender; MenuItem Close=NewMenuItem (); Close. Header="Delete"; //declaring MyCommand instancesClose.command =Closecmd; CLOSECMD.INPUTGESTURES.ADD (NewKeyGesture (KEY.D, Modifierkeys.alt));//Add shortcut keysClose.commandtarget = data;//Command function Targetcommandbinding CB=Newcommandbinding (); Cb.command=Closecmd; Cb. CanExecute+=Cb_canexecute; Cb. Executed+=cb_executed; DATA.COMMANDBINDINGS.ADD (CB); Data. ContextMenu=NewContextMenu (); Data. CONTEXTMENU.ITEMS.ADD (Close); Data. Contextmenu.isopen=true; } Private voidCb_executed (Objectsender, Executedroutedeventargs e) {ListBoxItem obj=(ListBoxItem) sender; This. ListView.Items.Remove (obj); E.handled=true; } Private voidCb_canexecute (Objectsender, Canexecuteroutedeventargs e) {E.canexecute=true; E.handled=true; }
Other implementations of command can be used depending on the situation, which facilitates manipulation of elements in the UI interface.
wpf--command commands in the right mouse button menu