I have two radio buttons, in the same group name, users selects either one (by default none is checked). When the user selects either one a string variable should store the content of the radio button. How to achieve this?
Asked
Active
Viewed 7,246 times
1
-
[This answer](http://stackoverflow.com/questions/9145606/how-can-i-reduce-this-wpf-boilerplate-code/9145914#9145914) might help, bind the `SelectedItem` to some property. – H.B. Feb 15 '12 at 15:24
-
It might if i actually understand!! i am new and so having tough time to get my head around simple answer would be nice tks – JackyBoi Feb 15 '12 at 15:29
1 Answers
3
First double click on the radiobutton(s) to get their checked events Then inside the eventhandler write this
<RadioButton Content="foo" Checked="radioButton1_Checked" />
<RadioButton Content="bar" Checked="radioButton1_Checked"/>
private void radioButton1_Checked(object sender, RoutedEventArgs e)
{
variable = (string)(sender as RadioButton).Content;
}

Ivan Crojach Karačić
- 1,911
- 2
- 24
- 44
-
-
You could probably use the event routing and handle Checked only on the parent container. – H.B. Feb 15 '12 at 15:26
-
@H.B. hi, would like to know is there anything wrong with the below code? public void radio_button_Checked(object sender, RoutedEventArgs e) { Object variable = (string)(sender as RadioButton).Content; } – JackyBoi Feb 15 '12 at 15:31
-
I tried to add Object and change to public rather than private and the thing i do this is because i want to use the variable in a button_click method. How to do that? – JackyBoi Feb 15 '12 at 15:32
-
1@JackyBoi: Introduce a [field](http://msdn.microsoft.com/en-us/library/ms173118.aspx) (and/or [property](http://msdn.microsoft.com/en-us/library/x9fsa0sw.aspx)) and assign to that. – H.B. Feb 15 '12 at 15:35
-
@H.B. dont need for that i have resolved it and can you please answer the question so that i can mark yours right! tks – JackyBoi Feb 15 '12 at 15:43