combobox - wpf listbox checkbox combination, really stuck -
i trying link checkbox , listview together, , use binding method set path object in order set check-box ischecked status in listbox.
list<test1> datas = new list<test1>(); var data = new test1 {key = 1, value = "hello", isselected= true}; datas.add(data); data = new test1 {key = 2, value = "hello2", isselected= false}; datas.add(data);
what need happen if checkbox checked ( isselected true ) need populate values , when click , unclick checkbox in gui need select proper listeview item can tag property.
this code below not set checkbox ischecked.
<listbox itemssource="{binding}" name="lstswimlane" width="225" height="125" selectionchanged="lstswimlane_selectionchanged"> <listbox.itemtemplate> <datatemplate> <stackpanel orientation="horizontal"> <checkbox ischecked="{binding relativesource={relativesource ancestortype=listboxitem}, path=isselected, mode=twoway}" checked="checkbox_checked" unchecked="checkbox_unchecked" /> <textblock text="{binding path=value}"></textblock> </stackpanel> </datatemplate> </listbox.itemtemplate> </listbox>
what need change in order set check box value in object? tried inotifychange etc...
here object well.
public class test1 : inotifypropertychanged { public event propertychangedeventhandler propertychanged; private bool _ischecked; public int key { get; set; } public string value { get; set; } public bool isselected { { return _ischecked; } set { if(_ischecked != value) { _ischecked = value; onpropertychanged("isselected"); } } } protected void onpropertychanged(string name) { if (propertychanged != null) propertychanged(this, new propertychangedeventargs(name)); } }
i new wpf, , stumped.
thanks.
do need work "three-way"? setting of 3 properties
listboxitem.isselected
checkbox.ischecked
item1.isselected
will affect both of other properties? unfortunately, there no such binding in wpf you're gonna have little work.
update
pointed out robert rossney, better solution bind
listboxitem.isselected
item1.isselected
checkbox.ischecked
listboxitem.isselected
updated xaml
<listbox itemssource="{binding}" name="lstswimlane" selectionmode="multiple" width="225" height="125" selectionchanged="lstswimlane_selectionchanged"> <listbox.itemcontainerstyle> <style targettype="listboxitem"> <setter property="isselected" value="{binding path=isselected, mode=twoway}"/> </style> </listbox.itemcontainerstyle> <listbox.itemtemplate> <datatemplate> <stackpanel orientation="horizontal"> <checkbox checked="checkbox_checked" unchecked="checkbox_unchecked" ischecked="{binding relativesource={relativesource ancestortype={x:type listboxitem}}, path=isselected}"> </checkbox> <textblock text="{binding path=value}"/> </stackpanel> </datatemplate> </listbox.itemtemplate> </listbox>
Comments
Post a Comment