问题描述
这与,不幸的是我很难总结我的题目中的问题 - 所以请与我一起。
我创建了一个名为的数据库类,它实现了一个
ITreeItem
接口,并从 BindingList< ITreeItem>
。我有一个第二个课程, TreeLeaf
,它是树中的一个叶子,一个不能包含更多子项的终端。
意图是能够将数据绑定到文件夹
,并具有数据绑定步行文件夹(和n级子文件夹)和树叶的树。然而,数据绑定不会下降到文件夹
的底层BindingList中,因此文件夹
在数据绑定时似乎没有子项目。
到目前为止,数据绑定正在使用文件夹作为 ITreeItem
,这是正确的,没有意识到文件夹
是除了 ITreeItem
的实例之外的任何东西。 / p>
我的问题是:我如何暴露数据绑定文件夹是 ITreeItem
,也是 BindingList< ITreeItem>
的后代;或者如何钩住数据绑定,以帮助它走下结构?
以下是到目前为止的实现示例:
使用System.ComponentModel;
使用System.Windows.Forms;
命名空间TreeData
{
public interface ITreeItem
{
string Name {get;组;
class TreeLeaf:ITreeItem
{
public TreeLeaf(string name)
{
Name = name;
}
public string Name {get;组; }
}
class文件夹:BindingList< ITreeItem>,ITreeItem
{
public Folder(string name)
{
Name = name ;
}
public string Name {get;组;
class示例
{
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;
//为了我的目的,'bindableControl'是
// Infragistics UltraTree或UltraGrid。
//bindableControl.DataSource = treeDataSource;
}
}
}
您可能需要考虑使用儿童属性?
public class文件夹:ITreeItem
{
public Folder(string name)
{
Name = name;
Children = new BindingList< ITreeItem>();
}
public string Name {get;组; }
public BindingList< ITreeItem>孩子{get;私人集合}
}
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;
}
}
}
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; }
}
这篇关于.NET WinForms DataBinding - BindingList< IBindableItem>其中IBindableItem的一些实现也可以实现IList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!