これは私の別の質問に関連しています。残念ながら、タイトルで問題を要約するのに苦労しています。ご容赦ください。
Folder
インターフェイスを実装し、ITreeItem
から継承するというデータバインド可能なクラスを作成しましたBindingList<ITreeItem>
。私は 2 番目のクラス を持っていますTreeLeaf
。これはツリーのリーフであり、これ以上子アイテムを含めることはできません。
Folder
その目的は、a にデータバインドし、データバインドでフォルダー (および n レベルのサブフォルダー) とリーフのツリーをたどることができるようにすることです。ただし、データ バインディングは の基になる BindingList に下がらずFolder
、その結果、Folder
データ バインド時に子項目がないように見えます。
これまでのところ、データ バインディングはフォルダーを のインスタンスとして処理しているように見えますがITreeItem
、これは正しく、Folder
が のインスタンス以外のものであることを認識していませんITreeItem
。
私の質問は次のとおりです。フォルダがの実装であり、その子孫でもあるデータバインディングにどのように公開できますか。または、データバインディングにフックして、構造をたどるのを助けるにはどうすればよいですか?ITreeItem
BindingList<ITreeItem>
これまでの実装のサンプルを次に示します。
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;
}
}
}