How to let C# designer edit my struct property?

一笑奈何 提交于 2019-12-14 01:28:05

问题


I am creating a custom Windows Forms control in C# with several custom properties. One of those properties is a simple struct with several integral fields:

public struct Test
{
    public int A, B;
}

Test _Test;

[Category("MyCategory")]
public Test TestProperty
{
    get { return _Test; }
    set { _Test = value; }
}

I want the Visual Studio designer to edit the fields of my structure the same way as it does for Size, Margins and other similar Windows Forms structures. Do I need to implement a custom property editor based on UITypeEditor class, or is there some common "structure editor" provided by .Net framework?


回答1:


This should do the trick:

[TypeConverter(typeof(ExpandableObjectConverter))]
public struct Test
{
    public int _A, _B;
    public int B
    {
        get { return _B; }
        set { _B = value; }
    }
    public int A
    {
        get { return _A; }
        set { _A = value; }
    }
}

Test _Test;

[Category("MyCategory")]
public Test TestProperty
{
    get { return _Test; }
    set { _Test = value; }
}



回答2:


You need to create your own designer. http://msdn.microsoft.com/en-us/magazine/cc164048.aspx



来源:https://stackoverflow.com/questions/6094595/how-to-let-c-sharp-designer-edit-my-struct-property

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