优化 PropertyGrid 类 [英] Optimize class for PropertyGrid

查看:32
本文介绍了优化 PropertyGrid 类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 GUI 项目中使用的库中定义了一个类 Foo.GUI 项目允许在 System.Windows.Forms.PropertyGrid 的帮助下对 Foo 实例进行图形编辑.

I have a class Foo defined in a library which is used in the GUI project. The GUI project allows graphical editing of an instance of Foo with the help of a System.Windows.Forms.PropertyGrid.

为了使在 PropertyGrid 中编辑类 Foo 的实例变得舒适,我必须为 Foo 的属性设置几个属性,例如 Browsable.

To make editing of an instance of class Foo in a PropertyGrid comfortable, I have to set several attributes to Foo's properties, Browsable for example.

但是,我不想在 Foo 中设置这些属性,因为它所在的库应该只有在代码中使用 Foo 所需的东西(而不是在 GUI 中).

However, I do not want to set up these attributes in Foo because the library it's in should have only the required things needed to use Foo in code (not in GUI).

如何获得 PropertyGrid 友好版本的 Foo?

How can I get a PropertyGrid-friendly version of Foo?

我已经尝试从它继承(命名为 FooDesignable)并用想要的属性遮蔽它的属性.然而,直到我发现 Foo 正在使用库项目的其他自定义类,然后我也不得不隐藏这些类,并更改 Foo 中的现有属性以返回 XxxDesignable 类型之前,这并没有很好地工作很长时间.

I already tried inheriting from it (naming it FooDesignable) and shadowing its properties with the wanted attributes. However, that didn't work well very long until I discovered that Foo is using other custom classes of the library project which I then also had to shadow, and change the existing properties in Foo to return the XxxDesignable types.

我在这里陷入死胡同了吗?还是我只是想坏了?

Am I in a dead end here? Or am I just thinking bad about it?

推荐答案

您可以做的是重用我在 SO 上对这个问题的回答中描述的 DynamicTypeDescriptor 类:找不到实体框架创建的属性的PropertyGrid Browsable,如何找到它?

What you could do is reuse the DynamicTypeDescriptor class described in my answer to this question here on SO: PropertyGrid Browsable not found for entity framework created property, how to find it?

例如:

public Form1()
{
    InitializeComponent();

    DynamicTypeDescriptor dt = new DynamicTypeDescriptor(typeof(MyBeautifulClass));

    // initialize the class the way you want
    MyBeautifulClass c = new MyBeautifulClass();
    c.MyProperty = "hello world";

    // we need to replace a property by another version, so let's remove the existing one
    dt.RemoveProperty("MyProperty");

    // create a new similar property with a new editor and the current value
    dt.AddProperty(
        typeof(string),            // type
        "MyProperty",              // name
        c.MyProperty,              // value
        "My Property",             // display name 
        "My Property Description", // description
        "My Category",             // category
        false,                     // has default value?
        null,                      // default value
        false,                     // readonly?
        typeof(MyEditor));         // editor 

    // create a wrapped object from the original one.
    // unchanged properties will keep their current value
    var newObject = dt.FromComponent(c);

    // hook on value change
    newObject.PropertyChanged += (sender, e) =>
    {
        // update the original object
        // note: the code could be made more generic
        c.MyProperty = newObject.GetPropertyValue<string>(e.PropertyName, null);
    };

    propertyGrid1.SelectedObject = newObject;
}

public class MyBeautifulClass
{
    public string MyProperty { get; set; }
}

// this stupid sample editor puts a current string in upper case... :-)
public class MyEditor : UITypeEditor
{
    public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
    {
        return UITypeEditorEditStyle.Modal;
    }

    public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
    {
        return value == null ? value : value.ToString().ToUpper();
    }
}

这篇关于优化 PropertyGrid 类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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