Modifying structure property in a PropertyGrid

后端 未结 3 1418
闹比i
闹比i 2020-12-20 20:48

Why SomeClass.ClassField.StructField property doesn\'t change in a propertyGrid? It seems, propertyGrid doesn\'t call SomeClass.

3条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-20 21:49

    I tweaked Simon Mourier's answer to avoid the need for ValueTypeTypeConverter to be a generic:

    public class ValueTypeTypeConverter : System.ComponentModel.ExpandableObjectConverter
    {
        public override bool GetCreateInstanceSupported(System.ComponentModel.ITypeDescriptorContext context)
        {
            return true;
        }
    
        public override object CreateInstance(System.ComponentModel.ITypeDescriptorContext context, System.Collections.IDictionary propertyValues)
        {
            if (propertyValues == null)
                throw new ArgumentNullException("propertyValues");
    
            object boxed = Activator.CreateInstance(context.PropertyDescriptor.PropertyType);
            foreach (System.Collections.DictionaryEntry entry in propertyValues)
            {
                System.Reflection.PropertyInfo pi = context.PropertyDescriptor.PropertyType.GetProperty(entry.Key.ToString());
                if ((pi != null) && (pi.CanWrite))
                {
                    pi.SetValue(boxed, Convert.ChangeType(entry.Value, pi.PropertyType), null);
                }
            }
            return boxed;
        }
    }
    

提交回复
热议问题