Binding to RadioButtons

Binding to RadioButtons

Old forum URL: forums.lhotka.net/forums/t/1285.aspx


Michael Hildner posted on Wednesday, September 20, 2006

Hi,

I was suprised I didn't find anything in a search. In a Windows Forms app, how do you go about binding a property to one of several RadioButtons? My property is a string.

The only thing I would know how to do, which is not binding,  is manually in the code, set the property when the checked value changes, and/or set the checked value when fetching an object.

Thanks,

Mike

Brian Criswell replied on Wednesday, September 20, 2006

I have not done much with it, but what I have done in the past is to add a boolean property for each radio button.  When a property is set to true, it sets the other property values to false before raising PropertyChanged.

tetranz replied on Wednesday, September 20, 2006

The best way I've found to bind to radio buttons is to build a user control.

The user control has a single property, in your case a string. Logic in the setter for that control sets the appropriate radio button checked on and the getter returns a value depending on which radio button is on.

Now you have only one property to bind to and you haven't polluted your business object with something that's really a UI issue.

Making that property an enum can be a tidy way to go.

Cheers
Ross

Michael Hildner replied on Wednesday, September 20, 2006

Thanks Brian and Ross for the replies,

I realized that I wanted to bind a string to something that needs to be boolean, or one of several booleans. It makes good sense to not have the business object be responsible for UI behaviour, so I'm going to attempt the UserControl route.

Thanks again,

Mike

Copyright (c) Marimer LLC