.net - WPF: How to make Contextmenu select and forget? -
i have contextmenu listbox inside. whenever right click control , select value contextmenu last selected value stays marked , can't select value again.
the idea is, may select same value within contextmenu in order turn property on or off.
should quite basic, missing?
many thanks,
edit: responses. have tried apply ideas without success. think major problem menuitems of contextmenu have no itemsource bound collection (e.g. possiblevalues in example).
may insert code clarification:
<border.contextmenu> <contextmenu> <contextmenu.itemcontainerstyle> <style targettype="{x:type menuitem}"> <setter property ="template"> <setter.value> <controltemplate targettype="{x:type menuitem}"> <contentpresenter x:name="header" contentsource="header" recognizesaccesskey="true"/> </controltemplate> </setter.value> </setter> </style> <contextmenu.itemcontainerstyle> <listbox borderthickness="0" width="35" margin="0" selecteditem="{binding path=volume, mode=twoway} datacontext="{binding relativesource={relativesource templatedparent}, path=datacontext}" itemssource="{binding path=possiblevalues}"/> </contextmenu> </border.contextmenu>
why have listbox in contextmenu? listbox allows select 1 item out of list (or multiple items, if enabled). example list of files in explorer.
a contextmenu itemscontrol, ie. can add arbitrary number of items it. add items directly.
edit: both contextmenu , menuitem itemscontrol. both have itemssource property. have following:
<contextmenu datacontext="{binding relativesource={relativesource templatedparent}, path=datacontext}" itemssource="{binding path=possiblevalues}"/>
possiblevalues collection of menuitems. each menuitem @ follows (here in code), example:
var menuitem = new menuitem(); menuitem.items.add(new textblock { text = "something" }); menuitem.command = dosomethingcommand; menuitem.commandparameter = "identifier";
edit2: try following. command use 1 of many implementations mvvm helper libraries, such delegatecommand<> prism or simplecommand cinch.
possiblevalues = new observablecollection<menuitem>(); // null value var nullmenuitem = new menuitem(); nullmenuitem.items.add(new textblock { text = "null" }); nullmenuitem.command = dosomethingcommand; nullmenuitem.commandparameter = null; possiblevalues.add(nullmenuitem); // values 1 9 (int = 1; < 9; i++) { var menuitem = new menuitem(); menuitem.items.add(new textblock { text = i.tostring() }); menuitem.command = dosomethingcommand; menuitem.commandparameter = i; possiblevalues.add(menuitem); }
as execute handler of command, along lines of:
public void dosomethingcommand_execute(object commandparameter) { this.selectednumber = commandparameter int?; // or whatever want in response selection. }
Comments
Post a Comment