#regionLoop-Generated right-click menuPrivateContextMenu Contextmenus (dictionary<string, routedeventhandler>list) {ContextMenu cm=NewContextMenu (); Style style= (Style) This. FindResource ("ContextMenu");//Define foreground display templatesCm. Style =style; foreach(vardcinchlist) {MenuItem Menu=NewMenuItem (); Menu. Style= (Style) This. FindResource ("Jjxymenuitem"); Menu. Header=DC. Key; Menu. Click+=DC. Value; Cm. Items.Add (menu); } returncm; } #endregion
Where to use the right-click menu:
dictionary<string, Routedeventhandler> dist =Newdictionary<string, routedeventhandler>(); //Add a right-click menu item, including name and eventDist. ADD ("Add", Add_click); Dist. ADD ("Modify", Edit_click); Dist. ADD ("Delete", Del_click); Dist. ADD ("Export", Putout_click); Dgrid. ContextMenu= Contextmenus (dist);
Add the Dgrid to the DataGrid instance object, use the dictionary, string as the menu name, and Routedeventhandler as the menu event.
WPF background Dynamic Build right-click menu