using System;
using System.Linq.Expressions;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public static Binding Add<T>
(this ControlBindingsCollection dataBindings,
object dataSource,
Expression<Func<Control, object>> controlExpression,
Expression<Func<T, object>> objectExpression)
{
return Add(dataBindings, dataSource, controlExpression, objectExpression, false);
}
public static Binding Add<T>
(this ControlBindingsCollection dataBindings,
object dataSource,
Expression<Func<Control, object>> controlExpression,
Expression<Func<T, object>> objectExpression,
bool formattingEnabled)
{
string controlPropertyName = ProcessExpression(controlExpression.Body);
string bindingTargetName = ProcessExpression(objectExpression.Body);
return dataBindings
.Add(controlPropertyName, dataSource, bindingTargetName, formattingEnabled);
}
public static Binding Add<T, K>
(this ControlBindingsCollection dataBindings,
object dataSource,
Expression<Func<K, object>> controlExpression,
Expression<Func<T, object>> objectExpression)
{
return Add(dataBindings, dataSource, controlExpression, objectExpression, false);
}
public static Binding Add<T, K>
(this ControlBindingsCollection dataBindings,
object dataSource,
Expression<Func<K, object>> controlExpression,
Expression<Func<T, object>> objectExpression,
bool formattingEnabled
)
{
string controlPropertyName = ProcessExpression(controlExpression.Body);
string bindingTargetName = ProcessExpression(objectExpression.Body);
return dataBindings.Add(controlPropertyName, dataSource, bindingTargetName, formattingEnabled);
}
private static string ProcessExpression(Expression expression)
{
string propertyName;
if (expression is MemberExpression)
{
propertyName = ((MemberExpression) (expression)).Member.Name;
}
else if (expression is UnaryExpression)
{
propertyName = ((MemberExpression) ((UnaryExpression) (expression)).Operand).Member.Name;
}
else
{
throw new InvalidOperationException(
"Unknown expression type error in DataBindingsExtensionMethods.Add<T, K>");
}
return propertyName;
}
}
txtBoundInt.DataBindings.Add<Contact>
(bindingSource, tb => tb.Text, contact => contact.Id);
或此:
cboBoundSelectedItem.DataBindings.Add
<Contact, ComboBox>
(bindingSource, cbo => cbo.SelectedItem, con => con.ContactType)
不过,似乎有很多表达方式。有更好的方法吗?
eDit:我确实找到了一种更好的方法 - 它是 @carl_g.
在下面撰写的将返回类型设置为对象是什么?
public static Binding Add<T>
(this ControlBindingsCollection dataBindings, object dataSource,
Expression<Func<Control, object>> controlLambda,
Expression<Func<T, object>> objectLambda) {
string controlPropertyName =
((MemberExpression)(controlLambda.Body)).Member.Name;
string bindingTargetName =
((MemberExpression)(objectLambda.Body)).Member.Name;
return dataBindings.Add
(controlPropertyName, dataSource, bindingTargetName);
}
问题已被编辑为仅包括一个答案,我在这里包括该答案。 作者可能应该离开Edit:我更喜欢这个解决方案,最终发现Google的Cache
(已从author的站点删除),因为它只需要一种类型的规范。我不知道为什么原始作者删除了它。
我已经使用Stuart发布的代码了几个月。 我确实添加了更多的过载,以匹配您可能要使用的数据指标方案(我只是在这里发布它,以便其他人更容易获得这一非常有用的事情)
public static class ControlExtensions {
/// <summary>Databinding with strongly typed object names</summary>
/// <param name="control">The Control you are binding to</param>
/// <param name="controlProperty">The property on the control you are binding to</param>
/// <param name="dataSource">The object you are binding to</param>
/// <param name="dataSourceProperty">The property on the object you are binding to</param>
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 Binding Bind<TControl, TDataSourceItem>(this TControl control, Expression<Func<TControl, object>> controlProperty, object dataSource, Expression<Func<TDataSourceItem, object>> dataSourceProperty, bool formattingEnabled = false)
where TControl :Control {
return control.DataBindings.Add(PropertyName.For(controlProperty), dataSource, PropertyName.For(dataSourceProperty), formattingEnabled);
}
public static Binding Bind<TControl, TDataSourceItem>(this TControl control, Expression<Func<TControl, object>> controlProperty, object dataSource, Expression<Func<TDataSourceItem, object>> dataSourceProperty, bool formattingEnabled, DataSourceUpdateMode updateMode)
where TControl :Control {
return control.DataBindings.Add(PropertyName.For(controlProperty), dataSource, PropertyName.For(dataSourceProperty), formattingEnabled, updateMode);
}
public static Binding Bind<TControl, TDataSourceItem>(this TControl control, Expression<Func<TControl, object>> controlProperty, object dataSource, Expression<Func<TDataSourceItem, object>> dataSourceProperty, bool formattingEnabled, DataSourceUpdateMode updateMode, object nullValue)
where TControl :Control {
return control.DataBindings.Add(PropertyName.For(controlProperty), dataSource, PropertyName.For(dataSourceProperty), formattingEnabled, updateMode, nullValue);
}
public static Binding Bind<TControl, TDataSourceItem>(this TControl control, Expression<Func<TControl, object>> controlProperty, object dataSource, Expression<Func<TDataSourceItem, object>> dataSourceProperty, bool formattingEnabled, DataSourceUpdateMode updateMode, object nullValue, string formatString)
where TControl :Control {
return control.DataBindings.Add(PropertyName.For(controlProperty), dataSource, PropertyName.For(dataSourceProperty), formattingEnabled, updateMode, nullValue, formatString);
}
public static Binding Bind<TControl, TDataSourceItem>(this TControl control, Expression<Func<TControl, object>> controlProperty, object dataSource, Expression<Func<TDataSourceItem, object>> dataSourceProperty, bool formattingEnabled, DataSourceUpdateMode updateMode, object nullValue, string formatString, IFormatProvider formatInfo)
where TControl :Control {
return control.DataBindings.Add(PropertyName.For(controlProperty), dataSource, PropertyName.For(dataSourceProperty), formattingEnabled, updateMode, nullValue, formatString, formatInfo);
}
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;
}
}
}