Reputation: 299
In my WPF Window i have RadioButton with default IsChecked property is true. How to write code for Uncheck the RadioButton. I tried with Sipmle example. I take one radiobutton.
<Grid>
<RadioButton Height="16" Margin="54,103,104,0" GroupName="t" Name="radioButton1" VerticalAlignment="Top" IsChecked="True" Unchecked="radioButton1_Unchecked">Text</RadioButton>
</Grid>
private void radioButton1_Unchecked(object sender, RoutedEventArgs e)
{
radioButton1.IsChecked = false;
}
But its not working Can any one help on this?
Ramki
Upvotes: 0
Views: 2436
Reputation: 16648
First of all, it is not possible to uncheck a radiobutton that is singled out. It would make no sense. That's why your event doesn't get fired.
Now, having a single RadioButton will not help you achieve anything: they are meant to be in a group of RadioButtons. Use a CheckBox instead.
Then in the UnChecked event, you don't have to uncheck it: the event is fired because it was unchecked in the first place =)
<Grid>
<CheckBox Height="16" Margin="54,103,104,0" GroupName="t" Name="checkBox1"
VerticalAlignment="Top" IsChecked="True" Unchecked="checkBox1_Unchecked">Text</CheckBox>
</Grid>
private void checkBox1_Unchecked(object sender, RoutedEventArgs e)
{
MessageBox.Show("checkBox1 has been unchecked!");
}
Upvotes: 3