Modifying structure property in a PropertyGrid

后端 未结 3 1420
闹比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:40

    In my case, the generic argument isn't known at compile time (options structure for a plugin). You can get a copy of the current value using context.PropertyDescriptor.GetValue(context.Instance); :

      public override object CreateInstance(ITypeDescriptorContext context, IDictionary propertyValues)
      {
         if (propertyValues == null)
            throw new ArgumentNullException("propertyValues");
    
         object boxed = context.PropertyDescriptor.GetValue(context.Instance);
         foreach (DictionaryEntry entry in propertyValues)
         {
            PropertyInfo pi = boxed.GetType().GetProperty(entry.Key.ToString());
            if (pi != null && pi.CanWrite)
               pi.SetValue(boxed, Convert.ChangeType(entry.Value, pi.PropertyType), null);
         }
         return boxed;
      }
    

提交回复
热议问题