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)));

Tags:

118 Responses to “Strongly typed databinding”

  1. TRAVIS says:

    Buy@Cheap.Accutane” rel=”nofollow”>..

    Buyno prescription tao…

  2. JASON says:

    Get@Accutane.Online” rel=”nofollow”>…

    Buywithout prescription ccw…

  3. RONNIE says:

    Accutane@10mg.20mg.Without.Prescription” rel=”nofollow”>..

    Buygeneric drugs ajd…

  4. MARION says:

    Cheap@Aciphex.Online” rel=”nofollow”>..

    Buyno prescription gdt…

  5. DALE says:

    Buy@Generic.Aciphex.Without.Prescription” rel=”nofollow”>…

    Buyit now knc…

  6. MARVIN says:

    Aciphex@20mg.Without.Prescription” rel=”nofollow”>.

    Buynow it lmg…

  7. DWIGHT says:

    Buy@Generic.Actonel.Without.Prescription” rel=”nofollow”>.

    Buygeneric drugs ouw…

  8. RICARDO says:

    Buy@Cheap.Advair” rel=”nofollow”>.

    Buygeneric drugs hwi…

  9. FRED says:

    Purchase@Advair.Online” rel=”nofollow”>..

    Buyno prescription jui…

  10. BRYAN says:

    Cheap@Advair.Online” rel=”nofollow”>.

    Buygeneric drugs nih…

  11. REGINALD says:

    < a href="http://trig.com/advair4746/biography/?ml=Generic-Advair-25mcg/50mcg-25mcg/125mcg-25mcg/250mcg-50mcg/500mcg-50mcg/100mcg-50mcg/250mcg-Without-Prescription Generic@Advair.25mcg50mcg.25mcg125mcg.25mcg250mcg.50mcg500mcg.50mcg100mcg.50mcg2

    Buydrugs without prescription jlg…

  12. ANGELO says:

    aciphex@cancer.buy” rel=”nofollow”>..

    Buygeneric drugs…

  13. JULIUS says:

    allopurinol@cheap.buy” rel=”nofollow”>..

    Buyit now…

  14. JOHNNY says:

    buy@amrut.fusion.in.india” rel=”nofollow”>…

    Buywithout prescription…

  15. BRYAN says:

    can@you.get.acai.berries.in.the.grocery.store” rel=”nofollow”>.

    Buydrugs without prescription…

  16. TYLER says:

    buy@real.strong.armour” rel=”nofollow”>.

    Buydrugs without prescription…

  17. greg says:

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

    Search rock US Charts…

  18. STEPHEN says:

    warfarin zoloft

    Buy_drugs without prescription…

  19. ROGER says:

    soft food diet ideas

    Buy_drugs without prescription…

  20. JEFFERY says:

    red wine and lipitor

    Buy_no prescription…

  21. FREDDIE says:

    combination food diet

    Buy_drugs without prescription…

  22. WADE says:

    faye sick lung cancer

    Buy_drugs without prescription…

  23. JAY says:

    effexor xr 75

    Buy_generic pills…

  24. DENNIS says:

    low fat diets history

    Buy_drugs without prescription…

  25. DONALD says:

    canine medi cam rx

    Buy_generic drugs…

Leave a Reply