.NET 속성 그리드. 그리드가 다른 방식으로 물체를 조작 할 수있는 방법이 있습니까?

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

문제

내가 이해했듯이, 속성 그리드에는 반사를 사용하여 특성을 추출하여 조작 할 수있는 객체가 제공됩니다.

내 문제는 런타임 중에 결정된 매개 변수 세트가 있으므로이 세트를 나타내는 속성이있는 클래스를 정적으로 작성할 수 없다는 것입니다.

나는이 문제를 해결하기 위해 두 가지 아이디어를 염두에두고 있지만 둘 다 복잡하고 아마도 많은 시간을 소비 할 것입니다. 하나는 클래스를 동적으로 정의하기 위해 반사 방출을 사용하고 다른 하나는 C# 소스 파일을 동적으로 빌드 한 다음 Codedom을 사용하여 컴파일하는 것입니다.

속성 그리드가 내 문제에 적합 할 수있는 다른 방식 (반사를 사용하여 물체의 속성을 추출 함)으로 행동 할 수 있습니까?

당신은 나에게 일을 할 수있는 다른 통제를 알고 있습니까?

처음부터 속성 그리드에 갔던 이유는 일반적인 유형에 대해 정말 멋진 데이터 검색 UI를 제공하는 능력 때문입니다. 가능하면 자동으로 이러한 것들을 얻고 싶습니다.

도움이 되었습니까?

해결책

예, PropertyGrid 물건을 표시 할 수 있습니다 다른 컴파일 타임 속성보다 TypeConverter, ICustomTypeDescriptor 또는 TypeDescriptionProvider 런타임 의사 속성을 제공합니다. 매개 변수의 모습을 보여줄 수 있습니까? 나는 예를 제공 할 수 있어야한다 ...


다음은 기본적인 예입니다 (모든 것이 있습니다 string, an, 등) 이전 답변 (관련이지만 다른) :

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