2017-03-15 5 views
0

사용자 정의 컨트롤에 대한 도움이 필요합니다. 사용자 정의 ControlDesigner가있는 사용자 정의 컨트롤이 있습니다.C# 사용자 정의 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 
    } 
} 
+1

, 오히려 사용 이벤트 ([MSDN] 참조 (https://msdn.microsoft.com/en-us/library /ms973820.aspx)), 이벤트에서 컨트롤의 인스턴스를 가져 와서 속성을 변경하고 거기에서 디자이너 속성을 조정할 수 있습니다. – Sinatr

+0

감사합니다 !!! 이것은 도움이되었습니다! 답변으로 게시하여 해결책으로 표시 할 수 있습니다. –

+0

나는 좋은 대답 (단지 힌트를 덧글으로 남길 수는 없다)을 게시 할 수는 없을 것이다. 하지만 [할 수있다] (http://stackoverflow.com/help/self-answer). – Sinatr

답변

1

: 여기

는 코드입니다. 힌트에 대한 Sinatr 덕분에 :

대답은 사용자 정의 컨트롤에서 ControlDesigner의 인스턴스에 속성을 설정하는 것입니다.

정적 클래스가 아닌 경우 =>이 경우 속성 변경은 내 문제가 있기 때문에 전역 적입니다. 여기

코드 샘플 :

당신은 정적 멤버를 통해 디자이너의 속성을 설정하지 않아야
//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; 
    } 
}