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
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.
Supposing I have a Windows Form with three 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 two radio buttons in Stack Overflow question How do I use databinding with Windows Forms radio buttons?, but I am missing the link while implementing the same with three buttons. I am having very erratic behaviors for the radio buttons.
I hope I was able to explain it 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.