问题描述
我有一个以特定方式实现属性的类,用于处理我们的业务逻辑系统的一些自定义需求。
这种具体方式在数据绑定的上下文中是操作的。基本上,如果我将一个类的对象放在一个表单上,数据绑定对对象没有什么意义,而该对象的属性检查器虽然列出了属性,却不允许我编辑它们。
我想知道的是,如果有一个接口或类似的,我可以在我的类中实现,将向表单设计者报告可以绑定哪些属性,并实现必要的自定义代码要代表我的属性与数据绑定系统通话。
请注意,我无需在属性检查器中编辑我的对象的属性值,这只是一个例子,说明这些属性的功能如何。我只需要数据绑定支持。
是否有这样的界面,如果是这样,那是什么?
我们假设我有这个课程:
public class CustomDataBinding:Component
{
private Dictionary< String,Object> _Properties = new Dictionary< String,Object>();
public CustomDataBinding()
{
_Properties [Property1] = 10;
_Properties [Property2] =Test;
}
}
有什么我可以做的对我的对象,短实际上做了这些属性,那会为这两个属性添加数据绑定支持?
请注意,对于我的真实类,我知道每个属性的类型,所以如果它更容易,请更改上述代码以仅使用字符串值:
public class CustomDataBinding:Component
{
私人字典< String,String> _Properties = new Dictionary< String,String>();
public CustomDataBinding()
{
_Properties [Property1] =Property1;
_Properties [Property2] =Property2;
}
}
我还需要验证支持,因为我们的课程报告每个属性的验证状态,但我已经实现了IDataErrorInfo和IDataValidationInfo,我认为会给我。我也实现了INotifyPropertyChanged,所以我得到了我想到的改变机制,但是我需要发现和讨论属性。除了这些接口,我应该研究的其他任何东西都是值得欢迎的信息。
听起来你正在描述一个物业袋;确实支持数据绑定,但您需要实现 ICustomTypeDescriptor
或 TypeDescriptionProvider
。然后通过 TypeDescriptor.GetProperties(obj)
或 TypeDescriptor.GetProperties(type)
定义可用的属性,或者在列表的情况,通过 ITypedList
。如果每个实例的属性更改,则适用于 ICustomTypeDescriptor
TypeDescriptionProvider
可以进行每个类型的自定义,并且可以像 BindingList< T>
这样的东西进行任何额外的工作。 p>
棘手的是,您需要定义每个属性的类型 - 如果您刚刚获得对象,则不简单
。 。
I have a class that implements properties in a specific way, to handle some custom requirements for our business logic system.
This "specific way" makes the properties non-operational in the context of data binding. Basically, if I drop an object of our class onto a form, data binding finds nothing on the object, and the property inspector for that object, though it lists the properties, doesn't allow me to edit them.
What I'm wondering is if there's an interface or similar that I can implement in my class that will report to the form designer what properties could be bound to, and that implements the custom code necessary to talk to the data binding system on behalf of my properties.
Note that I do not need to be able to edit the property values for my object in the property inspector, that was just an example of how non-functional the properties are. I just need the data binding support.
Is there such an interface, and if so, what is it?
Let's give a simple example.
Let's assume I have this class:
public class CustomDataBinding : Component
{
private Dictionary<String, Object> _Properties = new Dictionary<String, Object>();
public CustomDataBinding()
{
_Properties["Property1"] = 10;
_Properties["Property2"] = "Test";
}
}
Is there anything I could do to my object, short of actually making the properties, that would add data binding support for those two "properties"?
Note that for my real class, I know the type of every property, so if it makes it easier, change the above code to just use string values:
public class CustomDataBinding : Component
{
private Dictionary<String, String> _Properties = new Dictionary<String, String>();
public CustomDataBinding()
{
_Properties["Property1"] = "Property1";
_Properties["Property2"] = "Property2";
}
}
I will also need validation support, as our class can report validation status for every property as well, but I have already implemented IDataErrorInfo and IDataValidationInfo which I think will give me that. I have also implemented INotifyPropertyChanged, so I got the change mechanism in place I think, but I need to discover and talk to the properties. Beyond those interfaces, anything else I should look into would be welcome information.
It sounds like you are describing a property-bag; this is indeed supported for data-binding, but you need to implement ICustomTypeDescriptor
or TypeDescriptionProvider
. The available properties are then defined via TypeDescriptor.GetProperties(obj)
or TypeDescriptor.GetProperties(type)
- or in the case of lists, via ITypedList
. ICustomTypeDescriptor
is suitable if the properties change per-instance; TypeDescriptionProvider
can do per-type customisation, and works with things like BindingList<T>
without any extra work.
The tricky thing is that you need to define the type of each property - not simple if you've just got object
. Here is a very simplified example, that treats all properties as strings. In this example, because we are binding a list, it uses ITypedList
but doesn't implement ICustomTypeDescriptor
.
Note that for PropertyGrid
, a simpler shortcut is to use a TypeConverter
- this allows you to tweak the properties without having to use ICustomTypeDescriptor
or TypeDescriptionProvider
- see example.
这篇关于通过.NET中的某种接口使属性可用于数据绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!