属性描述符如何使用相同的代码行获取两个控件的值? [英] How can Property Descriptor gets values of two controls using same code line?
问题描述
我在前雇员为自定义 gridview 开发的项目之一中发现了此代码,该自定义控件工作正常,但我不确定它到底在做什么,
I found this code in one of the project developed by an Ex-Employee for custom gridview with custom controls which works fine but I am not sure what exactly it is doing,
代码:
public class aBoundField : ImageField
{
//here I got some get set properties defined
protected override void OnDataBindField(object sender, EventArgs e)
{
Control control = (Control)sender;
PropertyDescriptor propertyA = TypeDescriptor.GetProperties(DataBinder.GetDataItem(control.NamingContainer)).Find("boundField", true);
PropertyDescriptor propertyB = TypeDescriptor.GetProperties(DataBinder.GetDataItem(control.NamingContainer)).Find("boundField", true);
PropertyAFieldValue = this.GetValue(control.NamingContainer, this._PropertyAField, ref propertyA).ToString();
PropertyBFieldValue = this.GetValue(control.NamingContainer, this._PropertyBField, ref propertyB).ToString();
base.OnDataBindField(sender, e);
}
OnDataBindField
方法中发生了什么,尤其是在获取 PropertyDescriptor 时.我做了一些研究,发现它是一个属性包,但如果它是一个属性包,它如何知道这段代码中属性 A 或属性 B 的值是什么.
What's happening in OnDataBindField
method especially when it is getting PropertyDescriptor. I did bit research and figured out that it is a property bag, but if it is a property bag How would it know what value is for property A or Property B in this code.
PropertyDescriptor propertyA = TypeDescriptor.GetProperties(DataBinder.GetDataItem(control.NamingContainer)).Find("boundField", true);
PropertyDescriptor propertyB = TypeDescriptor.GetProperties(DataBinder.GetDataItem(control.NamingContainer)).Find("boundField", true);
我不太明白的是
Property Descriptor 如何使用相同的代码行获取两个控件的值
How can Property Descriptor gets values of two controls using same code line
TypeDescriptor.GetProperties(DataBinder.GetDataItem(control.NamingContainer)).Find("boundField", true)
上面的代码行将如何确定它是用于属性 A 还是属性 B.
How would above code line will figure out if its for Property A or Property B.
我试图从一个属性描述符中获取值,认为它是一个属性包,但它没有正常工作.
I tried to get values from one property descriptor thinking its a property bag but it didn't worked properly.
推荐答案
GetValue(control.NamingContainer, this._PropertyAField, ref propertyA)
ProperyA 作为参考,因此在该方法中发生在 propertyA 上的所有事情都将更新上面定义的 propertyA.
ProperyA is given as a reference therefor everything that happens to propertyA inside that method will update propertyA that was defined above.
使用
PropertyDescriptor propertyA = null;
代替
PropertyDescriptor propertyA = TypeDescriptor.GetProperties(DataBinder.GetDataItem(control.NamingContainer)).Find("boundField", true);
仍然有效.
进一步阅读
ref 方法参数关键字
这篇关于属性描述符如何使用相同的代码行获取两个控件的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!