正如我理解,该属性网格给出一个对象,它可以通过提取利用反射及其性质操纵

我的问题是我有一个在运行时确定的设定参数,因此我不能staticly构成一类具有属性来表示该集合。

我有两种方法来解决这个问题,但都非常复杂,可能会消耗大量的时间,事实上我会说,他们是不是在我的时间限制实用。一种方法是使用反射发出以便动态地定义一个类,另一个是到dynamiclly生成C#源文件,然后使用CodeDom中编译它。

属性网格可以以不同的方式表现(其他然后提取使用的反射的物体的性质),其可适合我的问题?

如果没有你知道的任何其他控制,可以为我做的工作吗?

我想说的是,我去从一开始的属性网格的原因是其共同types.For颜色提供很好的真的数据检索UI你autometically得到一个调色板,对于DATATIME你自动拥有一个漂亮的压光机的能力。我想自动把这些东西,如果可能的话。

有帮助吗?

解决方案

是,PropertyGrid可以显示的东西的其他不仅仅是编译时的特性,通过使用任何TypeConverterICustomTypeDescriptorTypeDescriptionProvider以提供运行时的伪特性。你可以给你的参数是什么样子的例子吗?我应该能够提供一个例子...


这里是一个基本的例子(一切是string等)的基础上的较早回复(相关但不同):

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
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)
    { // *** this controls whether it appears bold or not; you could compare
      // *** to a default value, or the last saved value...
        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); }
    }
}
[TypeConverter(typeof(PropertyBagConverter))]
class PropertyBag {
    public string[] GetKeys() {
        string[] keys = new string[values.Keys.Count];
        values.Keys.CopyTo(keys, 0);
        Array.Sort(keys);
        return keys;
    }
    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;
        }
    }
}
// has the job of (among other things) providing properties to the PropertyGrid
class PropertyBagConverter : TypeConverter {
    public override bool GetPropertiesSupported(ITypeDescriptorContext context) {
        return true; // are we providing custom properties from here?
    }
    public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, System.Attribute[] attributes) {
        // get the pseudo-properties
        PropertyBag bag = (PropertyBag)value;
        string[] keys = bag.GetKeys();
        PropertyDescriptor[] props = Array.ConvertAll(
            keys, key => new PropertyBagPropertyDescriptor(key));
        return new PropertyDescriptorCollection(props, true);
    }
}

static class Program {
    [STAThread]
    static void Main() { // demo form app
        PropertyBag bag = new PropertyBag();
        bag["abc"] = "def";
        bag["ghi"] = "jkl";
        bag["mno"] = "pqr";
        Application.EnableVisualStyles();
        Application.Run(
            new Form {
                Controls = { new PropertyGrid {
                    Dock = DockStyle.Fill,
                    SelectedObject = bag
                }}
            });
    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top