2

基本クラス(ノード)といくつかの継承されたタイプがあります。

Class Node
{
    Base_Attributes...
}

Class Derived : Node
{
    Derived_Attributes...
}

これらのタイプは、プロジェクトに追加したdllに含まれています。そして、その属性の1つがNodeであるというItemというクラスがあります。私は次のようにitmeのプロパティを表示するPropertygridを持っています:

Class Item
{
Point location;
String name;
Node quiddity;

bool[] IsBrowsable;

public Point Location{set;get;}
public String Name{set;get;}
public String NodeAttrib{set;get;}
[Browsable(IsBrowsable[thisindex])]
public String DerivedTypeAttribe{set;get;}
[Browsable(IsBrowsable[otheroneindex])]
public String DerivedTypeAttribe{set;get;}

Item(string type)
{
    switch(type)
    {
        case"some":
            Node = new derived_some();
            IsBrowsable[thisindex] = true;
            break;
    }
}
}

そしてメインフォームのどこか:

propertygrid.selectedobject = item;

ここでの問題は、派生型に指定されたいくつかのプロパティがあり、それらをpropetygridに表示する必要がありますが、ノードの型は実行時まで不明です。ブール値の配列を使用してBrowsabl()属性を設定しようとしましたが、BrowsableParameterは定数値である必要があることがわかりました。どのように私はこれを渡すことができますか?

4

1 に答える 1

1

TypeDescriptor;を介したフィルタリングの例を次に示します。もちろん、「表示するプロパティを知る方法」コードを変更することもできます。これは純粋に例示的なものです。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;

static class Program
{
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        PropertyGrid grid;
        using (var form = new Form
        {
            Controls = { (grid = new PropertyGrid { Dock = DockStyle.Fill}) }
        })
        {
            grid.SelectedObject = new Magic {ShowY = false};
            Application.Run(form);
        }
    }
}

[TypeConverter(typeof(MagicTypeConverter))]
public class Magic
{
    public Magic()
    {
        ShowX = ShowY = ShowZ = true;
    }

    public int A { get; set; }
    public bool B { get; set; }
    public string C { get; set; }
    public int X { get; set; }
    public bool Y { get; set; }
    public string Z { get; set; }

    [Browsable(false)]
    public bool ShowX { get; set; }
    [Browsable(false)]
    public bool ShowY { get; set; }
    [Browsable(false)]
    public bool ShowZ { get; set; }

    private class MagicTypeConverter : ExpandableObjectConverter
    {
        public override PropertyDescriptorCollection GetProperties(
             ITypeDescriptorContext context, object value,
             Attribute[] attributes)
        {
            var original = base.GetProperties(context, value, attributes);
            var list = new List<PropertyDescriptor>(original.Count);
            var magic = (Magic)value;
            foreach (PropertyDescriptor prop in original)
            {
                bool showProp = true;
                switch (prop.Name)
                {
                    case "X": showProp = magic.ShowX; break;
                    case "Y": showProp = magic.ShowY; break;
                    case "Z": showProp = magic.ShowZ; break;
                }
                if (showProp) list.Add(prop);
            }
            return new PropertyDescriptorCollection(list.ToArray());
        }
    }
}
于 2012-06-26T05:55:01.027 に答える