Ryland
Ryland

Reputation: 79

RadioButton.IsChecked Not Going False

I have a UWP app in C#. I have a Radio Button that the user can turn check or uncheck. It is checked by default and the user can uncheck it when they wish. However when they uncheck it the bool value for RadioButton.IsChecked never returns false making impossible for the user to then recheck the Radio Button if required. My code is below.

    private void usingmltBtn_Click(object sender, RoutedEventArgs e)
    {


        if (RadioButton.IsChecked == true)
        {
            RadioButton.IsChecked = false;
            i2cerrRec.Visibility = Visibility.Collapsed;
            i2cerrTxt.Visibility = Visibility.Collapsed;
            mltI2Cnck = 0;

        }
        else if (RadioButton.IsChecked == false)
        {
            RadioButton.IsChecked = true;
            mlttempLbl.Text = "N/C";
        }

    }

Thanks for your help.

Upvotes: 1

Views: 2104

Answers (4)

3vts
3vts

Reputation: 818

I can see 2 problems in here:

1- I might be wrong but the method you posted is triggered by a control whose name is usingltBtn (Not descriptive by the way) if this is the case you might be running this code with the trigger of another control.

2- Either the name of the RadioButton is usingltBtn or not you are not referencing the code to your control RadioButton is the name of a class not a specific control.

How to fix this? If the name of your control is usingltBtn use usingltBtn.IsChecked instead of RadioButton.IsChecked if not find the name of your control and use it

Upvotes: 0

James Croft
James Croft

Reputation: 1680

As other's have described, RadioButtons are designed to work in a group. This means that you will have multiple RadioButtons for a user to select from but they can only have one checked.

When a RadioButton is checked, it cannot be unchecked by the user by clicking it again without using a custom behaviour that checks if the user has tapped the RadioButton but this would not be recommended.

I suggest that you use the CheckBox for this particular functionality. You can even re-template a CheckBox control to look like a RadioButton if you wish.

Upvotes: 3

Dilip Jangid
Dilip Jangid

Reputation: 774

Go through the url below

https://learn.microsoft.com/en-us/windows/uwp/controls-and-patterns/radio-button

You might wrong in XAML code!

Upvotes: 1

Mizanur Rahman
Mizanur Rahman

Reputation: 272

Use this

 private void usingmltBtn_Click(object sender, RoutedEventArgs e)
        {

            if (RadioButton.Checked == true)
            {
                RadioButton.Checked = false;
                i2cerrRec.Visibility = Visibility.Collapsed;
                i2cerrTxt.Visibility = Visibility.Collapsed;
                mltI2Cnck = 0;

            }
            else if (RadioButton.Checked == false)
            {
                RadioButton.Checked = true;
                mlttempLbl.Text = "N/C";
            }
          }

Upvotes: 0

Related Questions