.Net Property Grid. Is there a way to let the Grid manipulate object in different way

前端 未结 1 1565
天命终不由人
天命终不由人 2021-01-07 08:35

As I understood , The property grid is given an object which it can manipulate by extracting its Properties using reflections.

My problem is that I have a set of Par

1条回答
  •  难免孤独
    2021-01-07 08:58

    Yes, PropertyGrid can display things other than just the compile-time properties, by using any of TypeConverter, ICustomTypeDescriptor or TypeDescriptionProvider to provide runtime pseudo-properties. Can you give an example of what your parameters look like? I should be able to provide an example...


    here's a basic example (everything is string, etc) based on an earlier reply (related but different):

    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 values
            = new Dictionary();
        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
                    }}
                });
        }
    }
    

    0 讨论(0)
提交回复
热议问题