您能否/如何在运行时为PropertyGrid(对于PCL)指定编辑器? [英] Can you/How to specify Editor at runtime for PropertyGrid (for PCL)?

查看:78
本文介绍了您能否/如何在运行时为PropertyGrid(对于PCL)指定编辑器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在其中编写了带有自定义对象的PCL,然后创建了一个用于处理PCL中的对象的GUI ...,然后尝试使用PropertyGrid编辑属性...我已经按顺序阅读了为了让网格知道如何处理对象,我需要指定EditorAttribute并提供TypeConverter ...但是我认为我不能在PCL中添加这两个...

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...

是否有一种方法可以在GUI级别处理此问题,例如告诉PropertyGrid在运行时使用特定类型的Editor/TypeConverter?我遍历了网格的可用功能/属性列表,但看起来不太可能.

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.

推荐答案

您可以创建一个元数据类,该元数据类包含与原始类相同的属性,并用某些属性修饰元数据类的属性.然后告诉类型描述符使用元数据类为原始类提供类型描述:

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));

PropertyGrid控件使用类的类型描述符来显示类的属性,其显示名称,其描述,其编辑器等.您可以通过不同的方式分配类型描述符.

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.

对于您来说,最好的解决方案是在运行时为您的课程注册一个新的TypeDescriptorProvider.这样,您可以在运行时简单地在PropertyGrid中更改类的外观.

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.

使用 AssociatedMetadataTypeTypeDescriptionProvider ,您可以为您的类创建类型描述符提供程序,该提供程序使用元数据类提供类型描述.然后,您可以使用 TypeDescriptor.AddProvider .

通过这种方式,您可以为包含属性属性的类引入元数据类.

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

  1. 向解决方案中添加一个可移植的类库,并向其中添加一个类:

  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; }
}

  • 将可移植类库的引用添加到Windows Forms项目.只需确保目标框架是一致的即可.

  • Add reference of the portable class library to your Windows Forms project. Just make sure that the target framework is consistent.

    向Windows窗体项目添加System.DesignSystem.ComponentModel.DataAnnotations引用.

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

    在Windows Forms Project中,为您的可移植类添加一个元数据类.该类应包含与原始类完全相同的属性:

    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; }
    }
    

    您需要添加以下用途:

    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.Design;
    using System.Drawing.Design; 
    

  • 在表单的Load事件中,通过以下方式为您的类型注册元数据提供程序:

  • 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));
    

  • 在属性网格中显示可移植类的实例:

  • 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:

    这篇关于您能否/如何在运行时为PropertyGrid(对于PCL)指定编辑器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆