c# - getting listbox.selectedindex by clicking on checkbox -
this user template in listbox. want have "listbox.selectedindex" on clicking checkbox of listbox. want knw of row,checkbox selected.like on click event of checkbox,it should focus whole selected row.
<listview x:name="listbox3" visibility="visible" margin="540,168,37,46" issynchronizedwithcurrentitem="true" borderbrush="black"> <listview.itemtemplate> <datatemplate> <stackpanel orientation="horizontal" width="200" > <textblock text="{binding vmname}" width="129" visibility="visible" /> <checkbox x:name="cb" isthreestate="false" ischecked="{binding ischeck, mode=twoway}" margin="6,0,18,6" horizontalalignment="stretch" verticalalignment="stretch"/> <checkbox x:name="cb1" isthreestate="false" ischecked="{binding ischeck1, mode=twoway}" horizontalalignment="stretch" verticalalignment="stretch" /> </stackpanel> </datatemplate> </listview.itemtemplate> </listview>
without a good, minimal, complete code example illustrates problem, difficult know advice useful you. without a clear problem statement, it's not clear want code do.
however, if understand correctly, displaying data item type, using corresponding datatemplate
object in listview
. user may toggle checkboxes, , want update selected item in listview
, item containing checkbox toggled.
there least couple of reasonable ways that. in both cases, set listview.selectedvalue
property data item object reference corresponding checkbox
being modified.
first way involves handling checked
, unchecked
events on checkbox
controls themselves, tracking listviewitem
, obtaining data item listviewitem
.
first, need write handler above:
private void cb_checked(object sender, routedeventargs e) { listviewitem listviewitem = getvisualancestor<listviewitem>((dependencyobject)sender); listbox3.selectedvalue = listbox3.itemcontainergenerator.itemfromcontainer(listviewitem); } private static t getvisualancestor<t>(dependencyobject o) t : dependencyobject { { o = visualtreehelper.getparent(o); } while (o != null && !typeof(t).isassignablefrom(o.gettype())); return (t)o; }
note helper method getvisualancestor<t>()
. uses visualtreehelper
walk tree listviewitem
object contains checkbox
control affected.
with object found, code calls itemcontainergenerator.itemfromcontainer()
find actual data item object reference, , assigns reference selectedvalue
property.
of course, handler useful, need subscribe relevant checked
, unchecked
events. example:
<datatemplate datatype="{x:type local:dataitem}"> <stackpanel orientation="horizontal" width="200" > <textblock text="{binding vmname}" width="129" visibility="visible" /> <checkbox x:name="cb" isthreestate="false" ischecked="{binding ischecked1, mode=twoway}" margin="6,0,18,6" horizontalalignment="stretch" verticalalignment="stretch" checked="cb_checked" unchecked="cb_checked"/> <checkbox x:name="cb1" isthreestate="false" ischecked="{binding ischecked2, mode=twoway}" horizontalalignment="stretch" verticalalignment="stretch" checked="cb_checked" unchecked="cb_checked"/> </stackpanel> </datatemplate>
(since didn't provide complete code example, in data item object class included, wrote own based on question. changed property names made more sense, i.e. ischecked1
, ischecked2
. feel free use own property names instead :) ).
second way little more direct in 1 respect, little less direct in respect. is, assuming data item object class implements inotifypropertychanged
, can subscribe propertychanged
event each data item object, , assign sender
of event listview.selectedvalue
property.
this more direct in don't have add code walks visual tree control's parent. it's less direct, in need code attaches necessary event handler each data item object.
an example of might this:
list<dataitem> dataitems = new list<dataitem> { new dataitem { vmname = "sagar" }, new dataitem { vmname = "kaustubh" }, new dataitem { vmname = "gaurav" }, new dataitem { vmname = "abhi" }, }; listbox3.itemssource = dataitems; propertychangedeventhandler handler = (sender, e) => listbox3.selectedvalue = sender; foreach (dataitem item in dataitems) { item.propertychanged += handler; }
note in above example, assign selectedvalue
property on any property change. in own code example, fine because properties can change checkbox-related ones. , of course, fine if want select corresponding listview
item on property value change. if want update on changes ischecked1
, ischecked2
properties, you'll need @ property name in handler. e.g.:
propertychangedeventhandler handler = (sender, e) => { if (e.propertyname == "ischecked1" || e.propertyname == "ischecked2") { listbox3.selectedvalue = sender; } }
here dataitem
class wrote code examples both approaches shown above:
class dataitem : inotifypropertychanged { private string _vmname; private bool _ischecked1; private bool _ischecked2; public string vmname { { return _vmname; } set { _vmname = value; onpropertychanged(); } } public bool ischecked1 { { return _ischecked1; } set { _ischecked1 = value; onpropertychanged(); } } public bool ischecked2 { { return _ischecked2; } set { _ischecked2 = value; onpropertychanged(); } } protected void onpropertychanged([callermembername] string propertyname = null) { propertychangedeventhandler handler = propertychanged; if (handler != null) { handler(this, new propertychangedeventargs(propertyname)); } } public event propertychangedeventhandler propertychanged; }
Comments
Post a Comment