简体   繁体   English

如何在设计时将复杂的属性值从一个用户控件复制到另一个用户控件?

[英]How to copy a complex property value from one user control to another as design time?

TL;DR; TL; DR;
How can I add copy-paste capability to a complex, multiple values property that will enable me to copy the property value from one user control and paste it to another at design time? 如何在复杂的多值属性中添加复制粘贴功能,使我能够在设计时从一个用户控件复制属性值并将其粘贴到另一个用户控件?

The long story 长话

I have created a user control ( StylableControl ) that has a complex property called Style . 我创建了一个用户控件( StylableControl ),它具有一个名为Style的复杂属性。

This property contains an instance of a class called StylableControlStyles , that contains multiple instances of a class called Style , where each one holds values such as BackColor , ForeColor , Image , Gradient (another class I've created) etc'. 该属性包含一个名为StylableControlStyles的类的实例,该类包含一个称为Style的类的多个实例,其中每个实例都包含诸如BackColorForeColorImageGradient (我创建的另一个类)等值。

I've also created a custom control designer to allow editing style property for the user control. 我还创建了一个自定义控件设计器,以允许编辑用户控件的style属性。 It shows a form where each style class in the style property can be edited easily. 它显示了一个表单,可轻松编辑style属性中的每个样式类。

Now I want to provide the users of this control an easy way to copy the entire content of the Style property from one instance of the user control to another instance, at design time. 现在,我想为该控件的用户提供一种简便的方法,在设计时将Style属性的整个内容从用户控件的一个实例复制到另一个实例。

I could, of course, override the ToString() method of the StylableControlStyles object to create a string representation that will encapsulate all the data saved in this object, but that would create a hugh string and of course would need a lot of work parsing it in the class converter (currenty I'm just using an ExpandableObjectConverter ). 当然,我可以重写StylableControlStyles对象的ToString()方法来创建一个字符串表示形式,该表示形式将封装保存在该对象中的所有数据,但这将创建一个很大的字符串,当然,需要大量的工作来解析它在类转换器中(当前我只是在使用ExpandableObjectConverter )。
I would like to avoid that if possible. 如果可能,我想避免这种情况。

Following Ash's advice in the comments I've used a DesignerVerb to copy and paste the Style to and from a private static member of type Style of the control designer. 遵循Ash在注释中的建议,我使用了DesignerVerb在控件设计器的Style类型的私有静态成员之间复制和粘贴Style

So in my control designer class I have: 因此,在我的控件设计器类中,我有:

private static ZControlStyle _CopiedStyle;

And have added these designer verbs: 并添加了以下设计师动词:

_Verbs.Add(new DesignerVerb("Copy Styles", CopyStyle));
_Verbs.Add(new DesignerVerb("Paste Styles", PasteStyle));

And the methods for copy ans paste: 以及复制和粘贴的方法:

private void PasteStyle(object sender, EventArgs e)
{
    if (_CopiedStyle != null)
    {
        var toggleButton = Control as ZToggleButton;
        if (toggleButton != null)
        {
            toggleButton.Style.FromStyle(_CopiedStyle);
        }
        else
        {
            (Control as ZControl).Style.FromStyle(_CopiedStyle);
        }

    }
}

private void CopyStyle(object sender, EventArgs e)
{
    var toggleButton = Control as ZToggleButton;
    if (toggleButton != null)
    {
        _CopiedStyle = toggleButton.Style;
    }
    else
    {
        _CopiedStyle = (Control as ZControl)?.Style;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM