Strongly typed databinding

Databinding proves to be very useful in some cases, but often turns out to be a maintenance nightmare. Since it requires strings with the property names to be passed as parameters of the Binding object. When the class, where a textbox is binded to, changes over time, so do property names. But since these property names are passed around as a string, they do not raise compile errors, causing unexpected behaviour of the application.

Here’s how you can make these bindings strongly typed, causing compilation errors if the property name of a class would change. Imagine we would like to bind a textbox to the Name property of the Person class, here’s the function that is going to help us. This function has a lambda expression returning a string and receiving a Person as parameter.

public string GetPropertyName(Expression<Func<Person, string>> propertySelector)
   MemberExpression memberExpression = propertySelector.Body as MemberExpression;
   MemberInfo propertyInfo = memberExpression.Member;
   return propertyInfo.Name;

The original binding code would like this, notice the “Name” string.

m_TextEditPerson.DataBindings.Add(new Binding("Text", m_Person, "Name"));

Here’s the new binding statement.

m_TextEditPerson.DataBindings.Add(new Binding("Text", m_Person, GetPropertyName(x => x.Name)));


120 Responses to “Strongly typed databinding”

  1. TERRY says:

    social interaction anxiety

    Buy_without prescription…

  2. RANDY says:

    at home diet programs

    Buy_generic pills…

  3. TERRENCE says:

    menopause thyroid cancer

    Buy_without prescription…

  4. SHANE says:

    sample ncp for diabetes

    Buy_without prescription…

  5. jordan says:

    Download@pop.Rock” rel=”nofollow”>.

    Buy rock US Charts…

  6. RAMON says:” rel=”nofollow”>…

    Buywithout prescription…

  7. cheryl alexander says:


    Its like you read my mind! You appear to know so much about this, like you wrote the book in it or something. I think that you could do with a few pics to drive the message home a bit, but other than that, this is wonderful blog. A great read. I’ll ce…

  8. NEIL says:

    CHEAP GENERIC PHARMACY : -==== Men’s Health Drugs ====-

    Buy Cheap Generic Drugs Now!…

  9. JULIAN says:

    Buy Viagra

    Check Unique Pharmacy Now!…

Leave a Reply