c# - handle check event on checkbox in wpf -
i've listview containing name , 2 checkboxes( name, male(checkbox),female(checkbox)). want make sure 1 checkbox selected listview @ time.
<usercontrol.resources> <datatemplate x:key="datatemp"> <stackpanel orientation="horizontal" width="200" > <textblock text="{binding vmname}" width="129" visibility="visible" /> <checkbox name="cb" isthreestate="false" checked="off_chek_select" ischecked="{binding ischeck, mode=twoway}" margin="6,0,18,6" horizontalalignment="stretch" verticalalignment="stretch"/> <checkbox name="cb1" isthreestate="false" checked="ins_chek_select" ischecked="{binding ischeck1, mode=twoway}" horizontalalignment="stretch" verticalalignment="stretch" /> </stackpanel> </datatemplate> </usercontrol.resources>
here, i've used datatemplate in listview. i'm not able access controls datatemplate writing on check event.
into tag
property can put reference parent element (in case it's stackpanel
) , in code-behind can find element using findname
method.
xaml:
<datatemplate x:key="datatemp"> <stackpanel x:name="stackpanel" orientation="horizontal" width="200" > <textblock text="{binding vmname}" width="129" visibility="visible" /> <checkbox name="cb" tag="{binding elementname=stackpanel}" isthreestate="false" checked="off_chek_select" ischecked="{binding ischeck, mode=twoway}" margin="6,0,18,6" horizontalalignment="stretch" verticalalignment="stretch"/> <checkbox name="cb1" tag="{binding elementname=stackpanel}" isthreestate="false" checked="ins_chek_select" ischecked="{binding ischeck1, mode=twoway}" horizontalalignment="stretch" verticalalignment="stretch" /> </stackpanel> </datatemplate>
code-behind:
private void off_chek_select(object sender, routedeventargs e) { var cbsender = sender checkbox; if (cbsender != null) { var stackpanel = cbsender.tag stackpanel; if (stackpanel != null) { var cb1 = stackpanel.findname("cb1") checkbox; if (cb1 != null) { cb1.ischecked = !cbsender.ischecked; } } } } private void ins_chek_select(object sender, routedeventargs e) { var cbsender = sender checkbox; if (cbsender != null) { var stackpanel = cbsender.tag stackpanel; if (stackpanel != null) { var cb = stackpanel.findname("cb") checkbox; if (cb != null) { cb.ischecked = !cbsender.ischecked; } } } }
if have multiple choices , 1 should selected @ time use radiobutton (msdn).
<usercontrol.resources> <datatemplate x:key="datatemp"> <stackpanel orientation="horizontal" width="200" > <textblock text="{binding vmname}" width="129" visibility="visible" /> <radiobutton name="cb" isthreestate="false" ischecked="{binding ischeck, mode=twoway}" margin="6,0,18,6" horizontalalignment="stretch" verticalalignment="stretch"/> <radiobutton name="cb1" isthreestate="false" ischecked="{binding ischeck1, mode=twoway}" horizontalalignment="stretch" verticalalignment="stretch" /> </stackpanel> </datatemplate> </usercontrol.resources>
Comments
Post a Comment