Home > database >  How to make radio buttons that you can unselect in wpf?
How to make radio buttons that you can unselect in wpf?

Time:09-11

I want to have a radio button group that you can unselect them. First I used radio but when you select a radio you can only unselect it by selecting another one. I want radio that you can unselect all of the options.

RadioButton textBox = new RadioButton();
textBox.Content = item;
textBox.GroupName = "Group1"   Id;
textBox.IsChecked = false;
textBox.Height = 20;

I also tried this.

CheckBox textBox = new CheckBox ();
textBox.Content = item;
textBox.GroupName = "Group1"   Id;
textBox.IsChecked = false;
textBox.Height = 20;

But unfortunately it throws an error saying that checkbox can't have a group name. If I admit the group name you can select more than one option. Is there any way you can have something that you can unselect an option but you wouldn't be able to select more than one option?

CodePudding user response:

Use a custom class instead of RadioButton:

    public class OffRadioButton : RadioButton
    {
        protected override void OnClick()
        {
            bool? isChecked = IsChecked;
            base.OnClick();
            if (isChecked == true)
                IsChecked = false;
        }
    }
  • Related