.NET WinForms DataBinding - BindingList<IBindableItem> where some implementations of IBindableItem may also implement IList

可紊 提交于 2019-12-11 10:20:37

问题


This is related to another question of mine, and unfortunately I'm having a difficult time summarizing the issue in my Title--so please bear with me.

I've created a databindable class called Folder which implements a ITreeItem interface and inherits from BindingList<ITreeItem>. I have a second class, TreeLeaf, which is a leaf in the tree, a terminus that can't contain more child items.

The intent is to be able to databind to a Folder and have databinding walk the tree of folders (and n-level subfolders) and leafs. The databinding, however, doesn't descend into the underlying BindingList of a Folder, and as a result the Folder is appearing to not have child items when databound.

So far it looks like databinding is working with the folder as an instace of ITreeItem, which is correct, and not realizing that the Folder is anything other than an instance of ITreeItem.

My question is: how can I either expose to databinding that a folder is both an implementation of ITreeItem and also a descendant of BindingList<ITreeItem>; or how can I hook into the databinding to help it walk down the structure?

Here is a sample of the implementation so far:

using System.ComponentModel;
using System.Windows.Forms;

namespace TreeData
{
    public interface ITreeItem
    {
        string Name { get; set; }
    }

    class TreeLeaf : ITreeItem
    {
        public TreeLeaf(string name)
        {
            Name = name;
        }

        public string Name { get; set; }
    }

    class Folder : BindingList<ITreeItem>, ITreeItem
    {
        public Folder(string name)
        {
            Name = name;
        }

        public string Name { get; set; }
    }

    class Example
    {
        public Example()
        {
            var rootFolder = new Folder("Root");
            var subFolder1 = new Folder("Folder1");
            var subFolder2 = new Folder("Folder2");

            rootFolder.Add(subFolder1);
            rootFolder.Add(subFolder2);

            subFolder1.Add(new TreeLeaf("Item1"));
            subFolder1.Add(new TreeLeaf("Item2"));

            subFolder2.Add(new TreeLeaf("Item3"));
            subFolder2.Add(new TreeLeaf("Item4"));

            var treeDataSource = new BindingSource();
            treeDataSource.DataSource = rootFolder;

            // For my purposes the 'bindableControl' is
            // an Infragistics UltraTree or UltraGrid.
            //bindableControl.DataSource = treeDataSource;
        }
    }
}

回答1:


You might want to consider having a Children property instead?

public class Folder : ITreeItem
{
    public Folder(string name)
    {
        Name = name;
        Children = new BindingList<ITreeItem>();
    }

    public string Name { get; set; }

    public BindingList<ITreeItem> Children { get; private set; }
}


来源:https://stackoverflow.com/questions/1295000/net-winforms-databinding-bindinglistibindableitem-where-some-implementation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!