欢迎来到 System.ComponentModel 的精彩世界。 .NET 的这个黑暗角落非常强大,但也非常复杂。
请注意;除非你有很多时间做这件事——你最好用你喜欢的任何机制简单地序列化它,但在每一端都将它重新水化为DataTable……接下来的内容不适合胆小的人;-p
首先 - 数据绑定(用于表格)适用于 lists (IList/IListSource) - 所以List<T> 应该没问题(编辑:我误读了一些东西)。但它不会理解你的字典实际上是列......
要让一个类型假装有列,您需要使用自定义 PropertyDescriptor 实现。有几种方法可以做到这一点,这取决于列定义是否始终相同(但在运行时确定,即可能来自配置),或者它是否随使用而变化(例如每个DataTable 实例如何具有不同的列)。
对于“每个实例”的自定义,您需要查看 ITypedList - 这个野兽(在 addition 到 IList 中实现)具有呈现表格数据属性的有趣任务...但它并不孤单:
对于“每种类型”的自定义,您可以查看TypeDescriptionProvider - 这可以建议类的动态属性...
...或者您可以实现ICustomTypeDescriptor - 但这仅在非常偶尔的情况下使用(用于列表)(对象索引器(public object this[int index] {get;}“)和至少一行绑定点的列表)。(此接口在绑定离散对象时更有用 - 即不是列表)。
实现ITypedList,并提供PropertyDescriptor 模型是一项艰巨的工作......因此只是偶尔进行。我对它相当熟悉,但我不会只是为了笑……
这是一个非常非常简单的实现(所有列都是字符串;没有通知(通过描述符),没有验证 (IDataErrorInfo),没有转换 (TypeConverter),没有额外的列表支持(IBindingList/IBindingListView),没有抽象(IListSource),没有其他元数据/属性等):
using System.ComponentModel;
using System.Collections.Generic;
using System;
using System.Windows.Forms;
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
PropertyBagList list = new PropertyBagList();
list.Columns.Add("Foo");
list.Columns.Add("Bar");
list.Add("abc", "def");
list.Add("ghi", "jkl");
list.Add("mno", "pqr");
Application.Run(new Form {
Controls = {
new DataGridView {
Dock = DockStyle.Fill,
DataSource = list
}
}
});
}
}
class PropertyBagList : List<PropertyBag>, ITypedList
{
public PropertyBag Add(params string[] args)
{
if (args == null) throw new ArgumentNullException("args");
if (args.Length != Columns.Count) throw new ArgumentException("args");
PropertyBag bag = new PropertyBag();
for (int i = 0; i < args.Length; i++)
{
bag[Columns[i]] = args[i];
}
Add(bag);
return bag;
}
public PropertyBagList() { Columns = new List<string>(); }
public List<string> Columns { get; private set; }
PropertyDescriptorCollection ITypedList.GetItemProperties(PropertyDescriptor[] listAccessors)
{
if(listAccessors == null || listAccessors.Length == 0)
{
PropertyDescriptor[] props = new PropertyDescriptor[Columns.Count];
for(int i = 0 ; i < props.Length ; i++)
{
props[i] = new PropertyBagPropertyDescriptor(Columns[i]);
}
return new PropertyDescriptorCollection(props, true);
}
throw new NotImplementedException("Relations not implemented");
}
string ITypedList.GetListName(PropertyDescriptor[] listAccessors)
{
return "Foo";
}
}
class PropertyBagPropertyDescriptor : PropertyDescriptor
{
public PropertyBagPropertyDescriptor(string name) : base(name, null) { }
public override object GetValue(object component)
{
return ((PropertyBag)component)[Name];
}
public override void SetValue(object component, object value)
{
((PropertyBag)component)[Name] = (string)value;
}
public override void ResetValue(object component)
{
((PropertyBag)component)[Name] = null;
}
public override bool CanResetValue(object component)
{
return true;
}
public override bool ShouldSerializeValue(object component)
{
return ((PropertyBag)component)[Name] != null;
}
public override Type PropertyType
{
get { return typeof(string); }
}
public override bool IsReadOnly
{
get { return false; }
}
public override Type ComponentType
{
get { return typeof(PropertyBag); }
}
}
class PropertyBag
{
private readonly Dictionary<string, string> values
= new Dictionary<string, string>();
public string this[string key]
{
get
{
string value;
values.TryGetValue(key, out value);
return value;
}
set
{
if (value == null) values.Remove(key);
else values[key] = value;
}
}
}