条件属性

时间:2012-06-24 10:34:07

标签: c# properties

我有一个基类(Node)和一些继承的类型。

Class Node
{
    Base_Attributes...
}

Class Derived : Node
{
    Derived_Attributes...
}

这些类型是我添加到项目中的dll。还有一个类让我们说Item的一个属性是Node。我有一个Propertygrid,我在其中显示itme的属性:

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;
    }
}
}

和mainform中的某个地方:

propertygrid.selectedobject = item;

这里的问题是为派生类型指定了一些属性,我需要在propetygrid中显示它们,但是直到运行时才知道节点的类型。我尝试使用布尔数组设置Browsabl()属性,但结果是可浏览参数需要是一个常量值。任何想法我怎么能通过这个?

1 个答案:

答案 0 :(得分: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());
        }
    }
}