Winform radiobutton data binding

Posted by Rajarshi on Stack Overflow See other posts from Stack Overflow or by Rajarshi
Published on 2010-02-15T16:33:02Z Indexed on 2010/04/09 18:43 UTC
Read the original article Hit count: 459

Filed under:
|
|

I am following the "Presentation Model" design pattern suggested by Martin Fowler for my GUI architecture in a Windows Forms project.

"The essence of a Presentation Model is of a fully self-contained class that represents all the data and behavior of the UI window, but without any of the controls used to render that UI on the screen. A view then simply projects the state of the presentation model onto the glass...." - Martin Fowler

Read more about this pattern at www.martinfowler.com/eaaDev/PresentationModel.html

I am finding the concept very fluid and easy to understand except this one issue of data binding RadioButtons to properties on the Data/Domain object.

Suposing I have a Windows Form with 3 radio buttons to depict some "Mode" options as -

  • Auto
  • Manual
  • Import

How can I use boolean properties on Data/Domain Objects to DataBind to these buttons? I have tried many ways but to no avail. For example I would like to code like -

rbtnAutoMode.DataBindings.Add("Text", myBusinessObject, "IsAutoMode"); rbtnManualMode.DataBindings.Add("Text", myBusinessObject, "IsManualMode"); rbtnImportMode.DataBindings.Add("Text", myBusinessObject, "IsImportMode");

There should be a fourth property like "SelectedMode" on the data/domain object which at the end should depict a single value like "SelectedMode = Auto". I am trying to update this property when any of the "IsAutoMode", "IsManualMode" or "IsImportMode" is changed, e.g. through the property setters. I have INotifyPropertyChanged implemented on my data/domain object so, updating any data/domain object property automatically updates my UI controls, that's not an issue.

There is a good example of binding 2 radio buttons here - http://stackoverflow.com/questions/344964/how-do-i-use-databinding-with-windows-forms-radio-buttons

but I am missing the link while implementing the same with 3 buttons. I am having very erratic behaviors for the Radio Buttons.

I hope I was able to explain reasonably. I am actually in a hurry and could not put a detailed code on post, but any help in this regard is appreciated.

There is a simple solution to this issue by exposing a method like -

public void SetMode(Modes mode) { this._selectedMode = mode; }

which could be called from the "CheckedChanged" event of the Radio Buttons from the UI and would perfectly set the "SelectedMode" on the business object, but I need to stretch the limits to verify whether this can be done by DataBinding.

© Stack Overflow or respective owner

Related posts about winforms

Related posts about radiobutton