.NETプロパティグリッド。グリッドは、別の方法でオブジェクトを操作できるようにする方法はあります

StackOverflow https://stackoverflow.com/questions/931644

質問

私は理解されるように、

、プロパティグリッドは、それが反射を使用して、そのプロパティを抽出することによって操作することができるオブジェクトが与えられます。

私の問題は、このように、私は静的にこのセットを表現するプロパティを持つクラスを構成することはできません、私は実行時に決定されたパラメータのセットを持っているということです。

私はこの問題を解決するために念頭に置いて2つの考えを持っていますが、両方は複雑であり、おそらく多くの時間を消費します、Infactは私は彼らが私の時間の制約の下では実用的ではないと言うだろう。一つは、動的にクラスを定義するために発光し、他はdynamiclly C#のソースファイルをビルドしてからのCodeDOMを使用して、それをコンパイルすることですリフレクションを使用することです。

プロパティグリッドは異なる方法で振る舞うことができる(他のその後の反射を使用して、オブジェクトのプロパティを抽出)私の問題を、スイートことができますか?

いいえあなたは私のために仕事をすることができ、他のコントロールを知っていますか?

私は初めからプロパティグリッドに行った理由は、あなたがautometicallyパレットを取得し、共通のtypes.For色のために本当に素敵なデータ検索UIを提供する能力だったと言いたい、dataTimeについてあなたは自動的に素敵なカレンダーを持っています。私は可能であれば、自動的にそれらのものを取得したいと思います。

役に立ちましたか?

解決

はい、PropertyGridを表示することができ、物事の他のよりちょうどコンパイル時のプロパティ、実行時の擬似特性を提供するためにTypeConverterICustomTypeDescriptorまたはTypeDescriptionProviderのいずれかを使用します。あなたのパラメータがどのように見えるかの例を与えることができますか?私は例を提供することができるはず...

<時間>

ここでの基本的な例は、href="https://stackoverflow.com/questions/882214/data-binding-dynamic-data/882246#882246"> 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