Edit: I prefer this solution I found eventually in Google's cache (it has been deleted from the author's site) as it only needs one type specification and does it in a way I haven't seen before. I don't know why the original author deleted it.
// Desired call syntax:
nameTextBox.Bind(t => t.Text, aBindingSource, (Customer c) => c.FirstName);
// Binds the Text property on nameTextBox to the FirstName property
// of the current Customer in aBindingSource, no string literals required.
// Implementation.
public static class ControlExtensions
{
public static Binding Bind<TControl, TDataSourceItem>
(this TControl control,
Expression<Func<TControl, object>> controlProperty,
object dataSource,
Expression<Func<TDataSourceItem, object>> dataSourceProperty)
where TControl: Control
{
return control.DataBindings.Add
(PropertyName.For(controlProperty),
dataSource,
PropertyName.For(dataSourceProperty));
}
}
public static class PropertyName
{
public static string For<T>(Expression<Func<T, object>> property)
{
var member = property.Body as MemberExpression;
if (null == member)
{
var unary = property.Body as UnaryExpression;
if (null != unary) member = unary.Operand as MemberExpression;
}
return null != member ? member.Member.Name : string.Empty;
}
}