c# - Expose boolean property in list of of objects to a WPF Checkbox -
i have working wpf mvvm app. i'm attempting expose list of mappable objects called targets. targets populated after app start. , exposed via viewmodel set targetfilter usercontrol data context. in targetfilter control listbox expose bool property called showonmap. able expose data via dictionary, clunky , not way turn targets on off. plus hacky , breaks databinding of mvvm.
public class target { public int index { get; set; } public int targetid { get; set; } public targetlocation lastlocation { get; set; } //lastlocation contains lat long etc. public string name { get; set} public bool showonmap { get; set; } }
on user session object have observablecollection:
public observablecollection<target> targetcache;
i have view model exposes data
public class mapviewmodel : viewmodelbase { private serversession _serversession; public observablecollection<target> gettargetcache { { //if (_serversession.user.targetcache != null) //{ return _serversession.user.targetcache; //} //return new observablecollection<target>(); } set { _serversession.user.targetcache = value; } } // key value pari of targetid, targetname public dictionary<string, string> getalltargetdictionary { { return _serversession.user.alltargetdictionary; } } // more code
}
this view model attached settings user control has number of child user controls
public partial class settings : usercontrol { private mapviewmodel _mapviewmodel; public event eventhandler dismissrequested; public settings() { initializecomponent(); } public settings(mapviewmodel mapviewmodel) { initializecomponent(); _mapviewmodel = mapviewmodel; targetfilter targetfilter = new targetfilter(_mapviewmodel.serversession); targetfilter.datacontext = _mapviewmodel; targetfilter.dismissrequestedtargetlist += new eventhandler(handleeventhideusercontrol); targetfiltertab.content = targetfilter;
my xaml trying show combo box , check box list of targets.
i have combo box working , bind dictionary, can't bind list:
<stackpanel grid.row="0"> <grid> <grid.columndefinitions> <columndefinition width="*" /> <columndefinition width="*" /> <columndefinition width="*" /> <columndefinition width="*" /> </grid.columndefinitions> <label grid.column="0" content="targets" fontweight="bold" /> <label grid.column="1" name="targetlist" horizontalcontentalignment="right" /> </grid> <textblock grid.column="0" verticalalignment="center" fontsize="14" margin="15,0,10,0">select case</textblock> <separator margin="10" /> <combobox grid.column="1" itemssource="{binding path=getcasedictionary}" displaymemberpath="value" selectedvaluepath="key" selectedvalue="{binding path=selectcase}" selectionchanged="combobox_selectionchanged" name="casedropdown" /> <textblock grid.column="0" verticalalignment="center" fontsize="14" margin="15,0,10,0">dispaly targets bind on dictionary</textblock> <separator margin="10" /> <listbox itemssource="{binding path=getalltargetdictionary}"> <listbox.itemtemplate> <datatemplate> <!--<checkbox ischecked="{binding showonmap, mode = twoway}" click="checkbox_click" name="value" > </checkbox>--> <checkbox content="{binding value}" ischecked="{binding path=key, mode=oneway}" click="checkbox_click"/> </datatemplate> </listbox.itemtemplate> </listbox> <textblock grid.column="0" verticalalignment="center" fontsize="14" margin="15,0,10,0">display targets bind on target cache</textblock> <separator margin="10" /> <listbox itemssource="{binding path=gettargetcache}" name="lbtargets"> <listbox.itemtemplate> <datatemplate> <!--<checkbox ischecked="{binding showonmap, mode = twoway}" click="checkbox_click" name="value" > </checkbox>--> <checkbox content="{binding name}" ischecked="{binding showonmap}" click="checkbox_click"/> </datatemplate> </listbox.itemtemplate> </listbox> </stackpanel>
here link looks no binding on final list box http://imgur.com/wr4lo1b
none of data present @ binding, , created , accessed later. why binding work dictionary not list? ideas make list bind?
thank you
i not see change notification on setter of viewmodels gettargetcache
property. think app not pick change / assignment of property @ runtime. have method raise change notification (inotifypropertychanged
) in base class viewmodelbase
can call? like
onpropertychanged("gettargetcache")
besides, sure binding first list box correct? can see first listbox
uses getalltargetdictionary
property on view model, of type dictionary<string, string>
. means binding
ischecked="{binding path=key, mode=oneway}"
refers key of dictionary, string.
but Ìschecked
bool....
have taken in debug window @ runtime? search binding error messsages there.
in addition recommend remove "get" prefix on properties, sounds method.
Comments
Post a Comment