c# - XAML using radio buttons to enable and disable textbox with databinding -
i trying figure out how enable , disable textboxes radio buttons , data binding. seems should able bind textbox isenabled boolean , modify value can't quite work. want have several sets of radio buttons , textboxes want generic way handle problem. tried use converter since not using x:names not sure helpful in case. can them enable/disable radio buttons not want do. code shows solution trying first textbox.
xaml code
<grid> <stackpanel> <radiobutton groupname="grp1" content="enable textbox" isenabled="{binding bttn1, mode=twoway}" ischecked="{binding bttn1}" checked="radiobutton_checked" unchecked="radiobutton_unchecked" /> <radiobutton groupname="grp1" content="disable textbox" isenabled="{binding bttn2}" /> <textbox isenabled="{binding txtbx1, mode=twoway}" bindinggroup="{binding grp1}" ></textbox> <radiobutton groupname="grp2" content="enable textbox" isenabled="{binding bttn3, mode=twoway}" ischecked="{binding bttn3}" checked="radiobutton_checked" unchecked="radiobutton_unchecked" /> <radiobutton groupname="grp2" content="disable textbox" isenabled="{binding bttn4}" /> <textbox isenabled="{binding txtbx2, mode=twoway}" bindinggroup="{binding grp2}" ></textbox> </stackpanel> </grid>
viewmodel code
private bool _bttn1; private bool _bttn2; private bool _bttn3; private bool _bttn4; private bool _txtbx1; private bool _txtbx2; public bool bttn1 { { return(_bttn1); } set { _bttn1 = value; _txtbx1 = false; raisepropertychanged(txtbx1.tostring()); } } public bool bttn2 { { return (_bttn1); } set { _bttn1 = value; _txtbx1 = false; raisepropertychanged(txtbx1.tostring()); } } public bool txtbx1 { { return (_txtbx1); } set { _txtbx1 = false; } }
further down have
public event propertychangedeventhandler propertychanged; protected void raisepropertychanged(string name) { if (propertychanged != null) { propertychanged(this, new propertychangedeventargs(name)); } } private void radiobutton_checked(object sender, routedeventargs e) { handle(sender radiobutton); } private void radiobutton_unchecked(object sender, routedeventargs e) { handle(sender radiobutton); } void handle(radiobutton radiobutton) { bool flag = radiobutton.ischecked.value; this.title = "ischecked = " + flag.tostring(); } public class radioconverter : ivalueconverter { public object convert(object value, type targettype, object parameter, cultureinfo culture) { return !(bool)parameter; } public object convertback(object value, type targettype, object parameter, cultureinfo culture) { return !(bool)value ? parameter : null; } }
in case want enable , disable textboxes radio buttons, solution works:
<grid> <stackpanel> <radiobutton x:name="rb1" groupname="grp1" content="enable textbox" /> <radiobutton groupname="grp1" content="disable textbox" /> <textbox isenabled="{binding ischecked, elementname=rb1}" ></textbox> <radiobutton x:name="rb2" groupname="grp2" content="enable textbox" /> <radiobutton groupname="grp2" content="disable textbox" /> <textbox isenabled="{binding ischecked, elementname=rb2}" ></textbox> </stackpanel>
Comments
Post a Comment