2012-10-18 112 views
5

例如 我想創建一個包含標籤和文本框的usercontrol(windows窗體)。 而我想公開兩個子控件作爲屬性,以便我可以 在客戶端表單設計器中設置子控件的屬性。如何在winforms設計器中公開用戶控件的整個子控件

所以代碼可能是這樣的:

 
public partial class LabelTextbox : UserControl 
    { 
     public LabelTextbox() 
     { 
      InitializeComponent(); 
     } 

     [ 
      Category("Appearance"), 
      Browsable(true), 
      Description("innerLabel") 
     ] 
     public DevComponents.DotNetBar.LabelX LabelPart 
     { 
      get 
      { 
       return this.labelx; 
      } 

      set 
      { 
       this.labelx = value; 
      } 
     } 


     [ 
      Category("Appearance"), 
      Browsable(true), 
      Description("InnerTextbox") 
     ] 
     public TextBox TextBoxPart 
     { 
      get 
      { 
       return this.textboxx; 
      } 

      set 
      { 
       this.textboxx = value; 
      } 
     } 
    } 

,然後我可以看到它在設計,它看起來像:

enter image description here

但是當我設置的用戶控件的內部標籤財產設計師, 它不能在designer.cs中創建關係代碼。 也就是說客戶端設置不被保存。

那麼我該如何解決這個問題。

順便說一句,我來自CN我的英語很差。任何人都可以回答我。

回答

6

裝飾你的子控件的屬性與DesignerSerializationVisibility屬性:

[ 
    Category("Appearance"), 
    Browsable(true), 
    Description("innerLabel"), 
    DesignerSerializationVisibility(DesignerSerializationVisibility.Content) //<-- here 
] 
public DevComponents.DotNetBar.LabelX LabelPart { 
    get { 
     return this.labelx; 
    } 
    set { 
     this.labelx = value; 
    } 
} 
+0

是啊,這就是我所需要的。 THKS。 – user1754971

相關問題