none
c# 如何将树型结构的节点名称和值用PropertyGrid控件动态显示 RRS feed

全部回复

  • http://blog.sina.com.cn/s/blog_6f14b7010101b91b.html

    类似于上面说的  但上面的没法完成类似树状的结构

    2020年2月29日 9:27
  • Hi huanyi00,

    根据我的搜索以及尝试,如果要做到动态这将很难。

    因为通常而言,设置PropertyGrid是需要获取明确类的成员,但是很明显,我们无法动态从树形结构中获取类的所有成员。

    况且如果ProgridGrid需要进行嵌套,需要对类设置TypeConverter。

    下面是我找的应该代码示例,你可以看下。

     public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                Product product = new Product
                {
                    Symbol = "test",
                    Details = new PartDetails
                    {
                        FileLineNo = 123,
                        Orientation = "up",
                        X = 555,
                        Y = 888
                    },
    
                    Fields = new FieldList {
                    new Field { Name = "One", Value = "Value 1" },
                    new Field { Name = "Two", Value = "Value 2" },
                    new Field { Name = "Three", Value = 1234 }
                }
    
                };
    
                propertyGrid1.SelectedObject = product;
                propertyGrid1.ExpandAllGridItems();
            }
        }
    
        public class Product
        {
    
            public string Symbol { get; set; }
    
            [TypeConverter(typeof(FieldListTypeConverter))]
            public FieldList Fields { get; set; }
    
            [TypeConverter(typeof(ExpandableObjectConverter))]
            public PartDetails Details { get; set; }
    
        }
    
        public class PartDetails
        {
            public int FileLineNo { get; set; }
            public int X { get; set; }
            public int Y { get; set; }
            public string Orientation { get; set; }
        }
    
        public class Field
        {
            public string Name { get; set; }
            public object Value { get; set; }
    
        }
    
        public class FieldList :
             List<Field>
        //, ICustomTypeDescriptor
        {
    
        }
    
        public class FieldListTypeConverter : TypeConverter
        {
            public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
            {
                if (destinationType != typeof(string))
                    return base.ConvertTo(context, culture, value, destinationType);
    
                return "";
            }
    
            public override bool GetPropertiesSupported(ITypeDescriptorContext context)
            {
                return true;
            }
    
            public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object obj, Attribute[] attributes)
            {
                List<PropertyDescriptor> pdList = new List<PropertyDescriptor>();
                List<Field> fields = obj as List<Field>;
                if (fields != null)
                {
                    foreach (Field field in fields)
                    {
                        FieldDescriptor fd = new FieldDescriptor(field);
    
                        pdList.Add(fd);
                    }
    
                }
                return new PropertyDescriptorCollection(pdList.ToArray());
            }
    
            private class FieldDescriptor : SimplePropertyDescriptor
            {
                public Field field { get; private set; } // instance
    
                public FieldDescriptor(Field field)
                    // component type, property name, property type
                    : base(field.GetType(), field.Name, field.Value.GetType())
                {
                    this.field = field;
                }
    
                public override object GetValue(object obj)
                {
                    return field.Value;
                }
    
                public override void SetValue(object obj, object value)
                {
                    field.Value = value;
                }
    
                public override bool IsReadOnly
                {
                    get { return false; }
                }
            }
        }

    结果示意图:

    Best Regards,

    Jack



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2020年3月2日 3:31
    版主