Can you/How to specify Editor at runtime for PropertyGrid (for PCL)?

不羁的心 提交于 2020-02-01 09:39:28

问题


I wrote a PCL with custom objects in there, and then I create a GUI that handles the objects from the PCL... and I try to use PropertyGrid to edit the properties... I've already read that in order for the grid to know what to do with the object, I need to specify the EditorAttribute as well as providing a TypeConverter... but I don't think I could add those 2 in the PCL...

Is there a way to handle this at the GUI level, like telling the PropertyGrid to use a specific type of Editor/TypeConverter at runtime? I went through the list of available function/properties of the grid and doesn't look like is possible.


回答1:


You can create a metadata class containing same properties as your original class and decorate properties of metadata class with some attributes. Then tell the type descriptor to use the metadata class for providing type description for your original class:

var provider = new AssociatedMetadataTypeTypeDescriptionProvider(
    typeof(MyClass),
    typeof(MyClassMetadata));
TypeDescriptor.AddProvider(provider, typeof(MyPortableClass));

The PropertyGrid control uses the type descriptor of your class to show the properties of your class, their display name, their description, their editor and so on. You can assign the type descriptor in different ways.

In your case, the best solution is registering a new TypeDescriptorProvider for your class at run-time. This way you can change the appearance of your class in PropertyGrid simply at run-time.

Using AssociatedMetadataTypeTypeDescriptionProvider you can create a type descriptor provider for your class that uses a metadata class to provide type description. Then you can register the provider using TypeDescriptor.AddProvider.

This way you can introduce a metadata class for your class that contains attributes for properties.

Step by Step Example

  1. Add a portable class library to solution and add a class to it:

    public class MyClass
    {
        public string Property1 { get; set; }
        public string Property2 { get; set; }
    }
    
  2. Add reference of the portable class library to your Windows Forms project. Just make sure that the target framework is consistent.

  3. Add System.Design and System.ComponentModel.DataAnnotations references to your Windows Forms Project.

  4. In Windows Forms Project, add a meta data class for your portable class. The class should contain exactly the same properties of your original class:

    public class MyClassMetadata
    {
        [Category("My Properties")]
        [DisplayName("First Property")]
        [Description("This is the first Property.")]
        public string Property1 { get; set; }
    
        [Category("My Properties")]
        [DisplayName("Second Property")]
        [Description("This is the second Property.")]
        [Editor(typeof(MultilineStringEditor), typeof(UITypeEditor))]
        public string Property2 { get; set; }
    }
    

    You need to add these usings:

    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.Design;
    using System.Drawing.Design; 
    
  5. In Load event of your form, register the metadata provider for your type this way:

    var provider = new AssociatedMetadataTypeTypeDescriptionProvider(
        typeof(MyClass),    
        typeof(MyClassMetadata));
    TypeDescriptor.AddProvider(provider, typeof(MyClass));
    
  6. Show an instance of your portable class in property grid:

    var myObject = new MyClass();
    this.propertyGrid1.SelectedObject = myObject ;
    

Here is the result after running the application:



来源:https://stackoverflow.com/questions/46099675/can-you-how-to-specify-editor-at-runtime-for-propertygrid-for-pcl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!