Binding a RadRadioButton

1 Answer 190 Views
Buttons, RadioButton, CheckBox, etc
Carl
Top achievements
Rank 1
Iron
Iron
Iron
Carl asked on 23 Mar 2023, 05:44 PM

I'm trying to bind a set of RadRadioButtons to object properties: Here is the binding:

rbOwnOrRentOwn.DataBindings.Add("IsChecked", prevAddress, nameof(prevAddress.OwnOrRentOwn), false, DataSourceUpdateMode.OnPropertyChanged);

rbOwnOrRentRent.DataBindings.Add("IsChecked", prevAddress, nameof(prevAddress.OwnOrRentRent), false, DataSourceUpdateMode.OnPropertyChanged);

and here are the properties:

        public bool OwnOrRentOwn

        {
            get => _ownOrRentOwn;
            set
            {
                _ownOrRentOwn = value;
                OnPropertyChanged(nameof(OwnOrRentOwn));
            }
        }

        public bool OwnOrRentRent
        {
            get => _ownOrRentRent;
            set
            {
                _ownOrRentRent = value;
                OnPropertyChanged(nameof(OwnOrRentRent));
            }
        }

When I bind like this the object properties are not set when the buttons are selected/deselected.

The docs say that CheckState is one way to go but these enums are defined in System.Windows.Forms. This would mean I need to introduce the System.Windows.Forms into my business layer. Is this what I need to do or is there a better way.

This should be just a simple data binding.

Thanks

Carl

 

1 Answer, 1 is accepted

Sort by
0
Dinko | Tech Support Engineer
Telerik team
answered on 27 Mar 2023, 12:59 PM

Hello Carl, 

Thank you for the provided code snippet.

I have tested your approach and the setter of the bound properties is called as expected. You can check the attached project which I used for testing purposes. In the project, you can observe that in the data-bound scenario, the radio button will be unchecked first then the new one will be checked. This behavior comes from the framework and it is not related to our controls. The same behavior can be observed with the native MS RadioButton. You could check this following thread which I believe will be quite useful for handling such a scenario.

Regards,
Dinko | Tech Support Engineer
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Carl
Top achievements
Rank 1
Iron
Iron
Iron
commented on 31 Mar 2023, 06:00 PM

We're using .NET 6 so not sure there that's an issue. It also seems that using non-nullable properties foxes the problem.

One question: In your demo, where the radio buttons are in container, I need to click 2x to select it. If I don't use a container its selects on the first click.

Why is that?

Carl

Dinko | Tech Support Engineer
Telerik team
commented on 03 Apr 2023, 01:17 PM

Are the radio buttons outside the container bounded? If yes, the same behavior will be observed. If there are not, you can check a radio button with one click as you mentioned.

I have also tested it in .Net 6 and I could not observe any difference in this behavior. I have also bound the RadRadioButtons to nullable properties. The setter of the properties is called as expected. The attached project is targeting Net6.

Tags
Buttons, RadioButton, CheckBox, etc
Asked by
Carl
Top achievements
Rank 1
Iron
Iron
Iron
Answers by
Dinko | Tech Support Engineer
Telerik team
Share this question
or