c# - WPF multiple checkbox check/uncheck -
i have 4 checkboxes 1 check , other 3 subcheckboxes. wanted check 3 subcheckboxes when check check , when deselect 1 check box main checkbox check unchecked only.
<window x:class="checkboxwpf.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" title="mainwindow" height="350" width="525"> <grid name="grdone"> <checkbox content="check" checked="checkbox1_checked" unchecked="checkbox1_unchecked" height="16" horizontalalignment="left" margin="39,23,0,0" name="checkbox1" verticalalignment="top"> </checkbox> <checkbox content="one" unchecked="checkbox2_unchecked" ischecked="{binding path=ischecked, elementname=checkbox1, mode=twoway}" height="16" horizontalalignment="left" margin="117,67,0,0" name="checkbox2" verticalalignment="top" /> <checkbox content="two" unchecked="checkbox3_unchecked" ischecked="{binding path=ischecked, elementname=checkbox1, mode=twoway}" height="16" horizontalalignment="left" margin="118,103,0,0" name="checkbox3" verticalalignment="top" /> <checkbox content="three" ischecked="{binding path=ischecked, elementname=checkbox1, mode=twoway}" height="16" horizontalalignment="left" margin="117,145,0,0" name="checkbox4" verticalalignment="top" /> <checkbox content="four" ischecked="{binding path=ischecked, elementname=checkbox1, mode=twoway}" height="16" horizontalalignment="left" margin="118,190,0,0" name="checkbox5" verticalalignment="top" /> </grid> </window>
public partial class mainwindow : window { public mainwindow() { initializecomponent(); } private void checkbox1_checked(object sender, routedeventargs e) { if (checkbox1.ischecked == true) { checkbox2.ischecked = true; checkbox3.ischecked = true; } } private void checkbox1_unchecked(object sender, routedeventargs e) { //if (checkbox1.ischecked == false) //{ // checkbox2.ischecked = false; // checkbox3.ischecked = false; //} } private void checkbox2_unchecked(object sender, routedeventargs e) { if (checkbox2.ischecked == false) { checkbox1.ischecked = false; } } private void checkbox3_unchecked(object sender, routedeventargs e) { if (checkbox3.ischecked == false) { checkbox1.ischecked = false; } } }
my checkbox grid
looks as:
<grid name="grdone"> <checkbox content="check" height="16" horizontalalignment="left" margin="24,44,0,0" name="checkbox1" verticalalignment="top" checked="checkbox1_checked" unchecked="checkbox1_unchecked" /> <checkbox content="one" height="16" horizontalalignment="left" margin="64,77,0,0" name="checkbox2" verticalalignment="top" checked="checkbox1_checked" unchecked="checkbox1_unchecked" /> <checkbox content="two" height="16" horizontalalignment="left" margin="64,99,0,0" name="checkbox3" verticalalignment="top" unchecked="checkbox1_unchecked" checked="checkbox1_checked" /> <checkbox content="three" height="16" horizontalalignment="left" margin="64,121,0,0" name="checkbox4" verticalalignment="top" checked="checkbox1_checked" unchecked="checkbox1_unchecked" /> <checkbox content="four" height="16" horizontalalignment="left" margin="64,143,0,0" name="checkbox5" verticalalignment="top" checked="checkbox1_checked" unchecked="checkbox1_unchecked" /> </grid>
and have 2 events, 1 checked
, unchecked
:
bool m_bchkupdating = false;
bool m_bunchkupdating = false;
private void checkbox1_checked(object sender, routedeventargs e) { checkbox chk = (checkbox)sender; if (!m_bchkupdating) { m_bchkupdating = true; switch (chk.name) { case "checkbox1": checkbox2.ischecked = true; checkbox3.ischecked = true; checkbox4.ischecked = true; checkbox5.ischecked = true; break; default: // chk.ischecked = true; if (checkbox2.ischecked == true && checkbox3.ischecked == true && checkbox4.ischecked == true && checkbox5.ischecked == true) { checkbox1.ischecked = true; } else { checkbox1.ischecked = false; } break; } m_bchkupdating = false; } } private void checkbox1_unchecked(object sender, routedeventargs e) { checkbox chk = (checkbox)sender; if (!m_bunchkupdating) { m_bunchkupdating = true; switch (chk.name) { case "checkbox1": checkbox2.ischecked = false; checkbox3.ischecked = false; checkbox4.ischecked = false; checkbox5.ischecked = false; break; default: // chk.ischecked = false; if (checkbox2.ischecked == false || checkbox3.ischecked == false || checkbox4.ischecked == false || checkbox5.ischecked == false) { checkbox1.ischecked = false; } else { checkbox1.ischecked = true; } break; } m_bunchkupdating = false; } }
Comments
Post a Comment