views:

282

answers:

1

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;
        }
    }
}
A: 

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; }
}
Brian
I ended up having to do something along these lines, basically adding an "Items" property of IBindingList to the base-class. Not quite what I was hoping for, but good enough for now.
STW