带有自定义 ControlDesigner 的 C# 用户控件每个副本

C# User control with custom ControlDesigner each copy

我需要一些有关用户控件的帮助。 我有一个具有自定义 ControlDesigner 的用户控件:

在设计器中,如果我在用户控件上更改特定 属性,我会更改 SelectionRules。

我遇到的问题如下: 当我放置此用户控件的多个副本并更改 属性 时,设计器将更改所有副本的 SelectionRules。

如何为每个副本设置它?

代码如下:

[Designer(typeof(BorderedTextBox_Designer))]
public partial class BorderedTextBox : UserControl
{
    [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
    [Description("Controls whether the text of the edit control can span more than one line.")]
    [Category("Behavior")]
    [DefaultValue(false)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public bool Multiline
    {
        set
        {
            //Set to TRUE:
            if (value)
            {
                BorderedTextBox_Designer.SelectionRule = BorderedTextBox_Designer.SelectionRulesEnum.All;
            }
            //Set to FALSE:
            else
            {
                BorderedTextBox_Designer.SelectionRule = BorderedTextBox_Designer.SelectionRulesEnum.RightLeft;
            }
        }
    }
}

internal class BorderedTextBox_Designer : ControlDesigner
{
    internal static SelectionRulesEnum SelectionRule;

    public override SelectionRules SelectionRules
    {
        get
        {
            switch (SelectionRule)
            {
                case SelectionRulesEnum.All:
                    return SelectionRules.Visible | SelectionRules.Moveable | SelectionRules.AllSizeable;
                case SelectionRulesEnum.UpDown:
                    return SelectionRules.Visible | SelectionRules.Moveable | SelectionRules.TopSizeable | SelectionRules.BottomSizeable;
                case SelectionRulesEnum.RightLeft:
                    return SelectionRules.Visible | SelectionRules.Moveable | SelectionRules.LeftSizeable | SelectionRules.RightSizeable;
                case SelectionRulesEnum.None:
                    return SelectionRules.Visible | SelectionRules.Moveable;
                default:
                    return SelectionRules.Visible | SelectionRules.Moveable;
            }
        }
    }

    internal enum SelectionRulesEnum
    {
        All,
        UpDown,
        RightLeft,
        None
    }
}

我已经弄明白了。感谢 Sinatr 的提示 :)

答案是将 属性 设置为用户控件中的 ControlDesigner 实例。

而不是静态 class => 在这种情况下,属性 更改将像我的问题一样是全局的。

这里有一个代码示例:

//The UserControl you created
[Designer(typeof(UserControlDesigner))]
[DefaultEvent("TextChanged")]
public partial class UserControl : UserControl
{
    //The property where we will store the UserControlDesigner instance when it is created
    internal UserControlDesigner Designer;

    //Initialize the UserControl
    public UserControl()
    {
        //Check for design time
        if (DesignMode)
            //Set the individual value to this UserControlDesigner instance's property
            Designer.DesignerProperty = 1;
    }
}

//The custom ControlDesigner should used to design your UserControl
internal class UserControlDesigner : ControlDesigner
{
    //The UserControl which is designed with this instance
    internal UserControl DesignedControl;

    //The Property you want to change each copy of your UserControl individually
    internal int DesignerProperty;

    //The method which is called when the UserControlDesigner instance is created
    public override void Initialize(IComponent component)
    {
        base.Initialize(component);

        //Cast the IComponent which is designed with this designer instance to your UserControl class
        DesignedControl = component as UserControl;

        //Check for successful cast
        if (DesignedControl != null)
            //Store this UserControlDesigner instance in the property "Designer" which we've created to access the instance in design time
            DesignedControl.Designer = this;
    }
}