c#winfrom PropertyGrid 运行时变更属性项

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PropertyGridCodeDemoByDynamic
{
    public partial class Form1 : Form
    {
        CustomObjectType obj = null;
        public Form1()
        {
            InitializeComponent();
            obj = new CustomObjectType
            {
                Name = "Foo",
                Properties =
                {
                    new CustomProperty { Name = "Bar", Type = typeof(int), Desc = "I'm a bar", DefaultValue=0},
                    new CustomProperty { Name = "EnumType", Type = typeof(EnumType), Desc = "choose the enumType", DefaultValue= EnumType.Age},
                    new CustomProperty { Name = "When", Type = typeof(DateTime), Desc = "When it happened",DefaultValue=System.DateTime.Now},
                }
            };
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.propertyGrid1.SelectedObject = obj;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (obj != null)
                obj.Properties.Add(new CustomProperty() { Name = "Add" + (obj.Properties.Count-2), Type = typeof(string), Desc = "I'm a bar", DefaultValue = "" });
            this.propertyGrid1.Refresh();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (obj != null)
            {
                int j = obj.Properties.Count - 3;
                int i = obj.Properties.FindIndex(p => p.Name.Equals("Add" + j, StringComparison.OrdinalIgnoreCase));
                if (i < 0)
                    return;
                obj.Properties.RemoveAt(i);
            }
            this.propertyGrid1.Refresh();
        }
    }

    [TypeConverter(typeof(CustomObjectType.CustomObjectConverter))]
    public class CustomObjectType
    {
        [Category("Standard")]
        public string Name { get; set; }

        [Category("Standard")]
        public string Label { get; set; }
        [Category("Standard")]
        public string Description { get; set; }
        private readonly List<CustomProperty> props = new List<CustomProperty>();
        [Browsable(false)]
        public List<CustomProperty> Properties { get { return props; } }

        private Dictionary<string, object> values = new Dictionary<string, object>();

        public object this[string name]
        {
            get
            {
                object val;
                values.TryGetValue(name, out val); return val;
            }
            set
            {
                values[name] = value;
            }
        }

        private class CustomObjectConverter : ExpandableObjectConverter
        {
            public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
            {
                var stdProps = base.GetProperties(context, value, attributes);
                CustomObjectType obj = value as CustomObjectType;
                List<CustomProperty> customProps = obj == null ? null : obj.Properties;
                PropertyDescriptor[] props = new PropertyDescriptor[stdProps.Count + (customProps == null ? 0 : customProps.Count)];
                stdProps.CopyTo(props, 0);
                if (customProps != null)
                {
                    int index = stdProps.Count;
                    foreach (CustomProperty prop in customProps)
                    {
                        props[index++] = new CustomPropertyDescriptor(prop);
                    }
                }
                return new PropertyDescriptorCollection(props);
            }
        }
        private class CustomPropertyDescriptor : PropertyDescriptor
        {
            private readonly CustomProperty prop;
            public CustomPropertyDescriptor(CustomProperty prop)
                : base(prop.Name, null)
            {
                this.prop = prop;
            }
            public override string Category { get { return "Dynamic"; } }
            public override string Description { get { return prop.Desc; } }
            public override string Name { get { return prop.Name; } }
            public override bool ShouldSerializeValue(object component) { return ((CustomObjectType)component)[prop.Name] != null; }
            public override void ResetValue(object component) { ((CustomObjectType)component)[prop.Name] = null; }
            public override bool IsReadOnly { get { return false; } }
            public override Type PropertyType { get { return prop.Type; } }
            public override bool CanResetValue(object component) { return true; }
            public override Type ComponentType { get { return typeof(CustomObjectType); } }
            public override void SetValue(object component, object value) { ((CustomObjectType)component)[prop.Name] = value; }
            public override object GetValue(object component)
            {
                return ((CustomObjectType)component)[prop.Name] == null ? prop.DefaultValue : ((CustomObjectType)component)[prop.Name];
            }
        }
    }


    public class CustomProperty
    {
        public string Name { get; set; }
        public string Desc { get; set; }
        public object DefaultValue { get; set; }
        Type type;

        public Type Type
        {
            get
            {
                return type;
            }
            set
            {
                type = value;
                //DefaultValue = Activator.CreateInstance(value);
            }
        }
    }


    public enum EnumType
    {
        Money,
        Message,
        Age,
        RealName
    }
}

代码来源于网上。有修改.关于PropertyGrid相关参见:http://www.cnblogs.com/mywebname/archive/2007/11/15/959732.html

代码下载:http://download.csdn.net/detail/fuweiping/9497360

猜你喜欢

转载自blog.csdn.net/fuweiping/article/details/51203890