简体   繁体   English

绑定到从CollectionBase派生的类的属性

[英]Binding to property of a class derived from CollectionBase

I am trying to bind some controls to an object - which is normally a pretty straightforward process. 我试图将某些控件绑定到一个对象-通常这是一个非常简单的过程。 Unfortunately, if the object that I'm binding to inherits from CollectionBase, binding to that classes fields causes the error: 不幸的是,如果我要绑定的对象是从CollectionBase继承的,则绑定到该类字段会导致错误:

Cannot bind to the property or column Caption on the DataSource. 无法绑定到数据源上的属性或列标题。 Parameter name: dataMember 参数名称:dataMember

Removing the collectionbase inheiritance makes this issue go away, but I need this object to be a collection. 删除collectionbase的不相容性会使此问题消失,但是我需要将此对象作为集合。 It seems as though CollectionBase causes higher level properties to become "unbindable." 似乎CollectionBase导致更高级别的属性变为“不可绑定”。 Is there some property I can override to fix this? 我可以重写某些属性来解决此问题吗? Any other ideas? 还有其他想法吗?

I found this example online that summarized the issue pretty easily. 我在网上找到了这个例子,可以很容易地总结出问题。 Unfortunately, I have yet to find an answer in all the places I've seen this example posted. 不幸的是,我还没有在所有看到此示例的地方找到答案。

Code: 码:

[STAThread]
static void Main()
{
    TestCollection obj = new TestCollection();
    using (Form f = new Form())
    using (BindingSource bs = new BindingSource())
    {
        bs.DataSource = typeof(Test);
        f.DataBindings.Add("Text", bs, "Caption");
        bs.DataSource = obj; // breaks

        //List<TestallData = new List<Test>();
        //allData.Add(obj);
        //bs.DataSource = allData;
        f.ShowDialog();
    }
}

class TestCollection : CollectionBase
{
    public string Caption { get { return "Working"; } }
}

CollectionBase provides interfaces for a List of Objects, as such when used as a datasource the binding tries to look inside the list for the individual binding data. CollectionBase为对象列表提供接口,例如,绑定用作数据源时,绑定会尝试在列表内部查找单个绑定数据。 When there is no list, you have a problem. 没有列表时,您有问题。

If you want a the caption and you want to use CollectionBase you should have 2 classes involved, not just one. 如果要添加字幕,并且要使用CollectionBase ,则应该涉及2个类,而不仅仅是一个。

public class TestObj
{
    public string caption { get { return "yay"; } }
}

public class TestCol : CollectionBase
{
    //methods that implement CollectionBase for the TestObj type
}

with those two you can bind one of two ways. 使用这两种方法,您可以绑定以下两种方法之一。

TestObj obj = new TestObj();
TestCol col = new TestCol();
col.Add(obj);

//bind to obj, OR bind to col.  Both would work with this setup.

http://msdn.microsoft.com/en-us/library/system.collections.collectionbase%28v=vs.90%29.aspx http://msdn.microsoft.com/zh-cn/library/system.collections.collectionbase%28v=vs.90%29.aspx

There is a sample implementation of CollectionBase there. 那里有一个CollectionBase的示例实现。

UPDATE: EDITED FROM COMMENT 更新:根据评论编辑

There isn't any method that I personally know which allows you to bind to the outer properties of a collection. 我个人没有任何方法可以绑定到集合的外部属性。 As a workaround, you can use a 3 class system (yea, I know, more and more complicated). 作为一种解决方法,您可以使用3类系统(是的,我知道,越来越复杂)。

public class TestHeader
{
    public string Data {get;set;}
}

public class TestCol : CollectionBase
{
    //...
}

public class TestObj
{
    public TestHeader header {get;set;}
    public TestCol col {get;set;}
}

bind the outer fields to TestObj.header and bind the collection fields to TestObj.col . 将外部字段绑定到TestObj.header ,将集合字段绑定到TestObj.col This is a workaround, but as stated I dont actually know a way to directly implement what you seem to want. 这是一种解决方法,但是如上所述,我实际上不知道直接实现您似乎想要的方法。 I wish I did, There are portions of my own code that would benefit from it. 我希望自己做到了,我自己的代码中有一部分可以从中受益。

Another Example 另一个例子

You could also do it with two classes, but you would still need to nest the collection itself 您也可以使用两个类来完成此操作,但是仍然需要嵌套集合本身

public class TestObj
{
    public string data {get;set;}
    public TestCol col {get;set;}
}

In this case, bind single data fields to TestObj , and collection fields to TestObj.col 在这种情况下,将单个数据字段绑定到TestObj ,将收集字段绑定到TestObj.col

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM