HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

'Universal' WinForms TextBox Text 'Two-Way' Binder

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
universaltextwaywinformstwotextboxbinder

Problem

Below is the code of a custom C# class I have called UniversalTextBoxTextBinder. I'm interested to hear the code review comments to find out the limitations of this 'universal' solution as well as to get posted here the references on similar solutions. Obviously the solution posted here can be generalized on other types of WinForms controls, WPF(?) controls etc.

NB: This solution would not see the light without Hans Passant hint:

```
///
/// Dynamically binds 'two-way' the properties' values of a (POCO) instance of
/// to the host control textboxes' 'Text' property.
///
///
public class UniversalTextBoxTextBinder
{
public UniversalTextBoxTextBinder(
Control hostControl,
T boundObject,
string textBoxNamePrefix = default(string),
string textBoxNameSuffix = "TextBox"
)
{
_hostControl = hostControl;
this.BoundObject = boundObject;
_expandoObject = new ExpandoObject();

IDictionary dictionary = _expandoObject as IDictionary;

foreach (PropertyInfo propertyInfo in this.BoundObject.GetType().GetProperties())
{
if (propertyInfo.CanRead && propertyInfo.CanWrite)
{
dictionary[propertyInfo.Name] = propertyInfo.GetValue(this.BoundObject);
bind(propertyInfo);
}
}

((INotifyPropertyChanged)_expandoObject).PropertyChanged += new PropertyChangedEventHandler(propertyChanged);

}

private Control _hostControl;
public T BoundObject { get; private set; }
private dynamic _expandoObject;

private void bind(
PropertyInfo propertyInfo,
string controlPropertyNameToBindTo = "Text",
string textBoxNamePrefix = default(string),
string textBoxNameSuffix = "TextBox"
)
{
try
{
bindExpandoField(textBoxNamePrefix + propertyInfo.Name + textBoxNameSuffix,

Solution

I think this code, while fairly impressive, is reinventing a wheel that WinForms wasn't designed to use. Trying to implement the Model-View-ViewModel pattern with WinForms will cause you more trouble than you can possibly imagine, if your application is just a little bit more complex than, say, a calculator app.

You're using reflection quite extensively, so your binding is inherently slow, which means your View has to be as simple as possible, because binding many controls will be more painful than it should.

WinForms applications are better off with the Model-View-Presenter pattern.

That said your naming and indentation aren't always consistent in terms of PascalCasing method names and line breaks before/after } and ).

I know @HighCore can be a little harsh at times, but he knows his stuff and he's absolutely right: if you want to simulate WPF behavior in WinForms, the real thing will be much less trouble, and leave you with much cleaner code.

Sorry if that's not what you wanted to hear...

Context

StackExchange Code Review Q#23847, answer score: 5

Revisions (0)

No revisions yet.