本文介绍了强类型Windows窗体数据绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用扩展方法查找强类型的Windows Forms数据绑定。以下是的帮助,如下所示:
I am looking into strong typed Windows Forms databinding using extension methods. I have got this far following help from Xavier as below:
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;
}
}
现在我可以这样设置一个DataBinding: / p>
Now I can set up a DataBinding like this:
txtBoundInt.DataBindings.Add<Contact>
(bindingSource, tb => tb.Text, contact => contact.Id);
或者这个:
cboBoundSelectedItem.DataBindings.Add
<Contact, ComboBox>
(bindingSource, cbo => cbo.SelectedItem, con => con.ContactType)
似乎有很多表达式的表现。有没有更好的方法?
There seems to be a lot of casting of expressions going on though. Is there a better way?
编辑:我找到了一个更好的方法,但我遇到麻烦改变这个问题为此答案 - 这是 by @Carl_G。
I did find a better way, but I got into trouble for changing this question to that answer - it's reproduced below by @Carl_G.
推荐答案
如何将返回类型设置为对象?
What about setting the return type to object?
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);
}
这篇关于强类型Windows窗体数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!