2015-09-24 84 views
1

這裏是情況。我做了一個自定義按鈕控制:c#winform:覆蓋自定義控件設計器中的默認屬性

public partial class EButton : Control, IButtonControl 

此控件包含一個按鈕。我使用的getter/setter方法在設計師編輯自己的屬性,如:

[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)] 
[Browsable(true)] 
public UIButton Button 
{ 
    get 
    { 
     return EBtn; 
    } 
    set 
    { 
     EBtn = value; 
    } 
} 

現在,我可以訪問所有的設計師我的按鈕的屬性。

我的問題是,無論我定義什麼,這總是被我的控件中的默認屬性覆蓋。

例如: 在我的控制中,按鈕的BackColor設置爲白色。 在特定的表單中,我想將此按鈕設置爲紅色,因此我在表單的設計器中將BackColor屬性設置爲紅色。 當我重新加載設計器時,該值已返回到白色。

我不想爲按鈕的每個屬性製作setter。這是一個特定的控制(http://www.janusys.com/controls/),它有很多有用的屬性,我想適應每個特定的情況。

有誰知道解決方案嗎?

+0

看看Plutonix的答案在這裏:http://stackoverflow.com/questions/32070817/abbreviate-serialization-in-designer你最有可能實現爲您的新控件提供一個類型轉換器,告訴設計師如何使用它。 – Jens

+0

http://stackoverflow.com/questions/30575572/when-compiling-some-property-values-are-reset-set-in-the-designer – kevintjuh93

+0

*當我重新加載設計器時,該值已返回到白*聽起來像序列化問題。嘗試檢查設計器文件生成的文件,看看是否實際保存了此更改。 – Sinatr

回答

1

您應該使用[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]

[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] 
public Button MyButtonProperty 
{ 
    get 
    { 
     return this.button1; 
    } 
    set 
    { 
     value = this.button1; 
    } 
} 

使用DesignerSerializationVisibilityDesignerSerializationVisibility.Content,要表示該物業包括內容的,應爲每個市民,不是隱藏的對象分配給屬性生成初始化代碼屬性。

這裏是一個獨立的測試:

using System.ComponentModel; 
using System.Windows.Forms; 

namespace MyControls 
{ 
    public partial class MyUserControl : UserControl 
    { 
     private System.ComponentModel.IContainer components = null; 
     protected override void Dispose(bool disposing) 
     { 
      if (disposing && (components != null)) 
      { 
       components.Dispose(); 
      } 
      base.Dispose(disposing); 
     } 

     #region Component Designer generated code 
     private void InitializeComponent() 
     { 
      this.button1 = new System.Windows.Forms.Button(); 
      this.SuspendLayout(); 
      // 
      // button1 
      // 
      this.button1.Location = new System.Drawing.Point(3, 15); 
      this.button1.Name = "button1"; 
      this.button1.Size = new System.Drawing.Size(75, 23); 
      this.button1.TabIndex = 0; 
      this.button1.Text = "button1"; 
      this.button1.UseVisualStyleBackColor = true; 
      // 
      // MyUserControl 
      // 
      this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F); 
      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 
      this.Controls.Add(this.button1); 
      this.Name = "MyUserControl"; 
      this.ResumeLayout(false); 
     } 
     #endregion 
     private System.Windows.Forms.Button button1; 
     public MyUserControl() 
     { 
      InitializeComponent(); 
     } 

     [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] 
     public Button MyButtonProperty 
     { 
      get 
      { 
       return this.button1; 
      } 
      set 
      { 
       value = this.button1; 
      } 
     } 
    } 
} 
+0

差不多就是這樣。我用你說的和現在,我所有的屬性都重置。 我需要的是使用在控制設計器中定義的默認值,可以在表單設計器中更改它們 – Robouste

+1

@Robouste我已經用這種方式測試瞭解決方案,首先創建一個'UserControl'然後添加一個按鈕,然後添加屬性,然後重建解決方案,然後把你的'UserControl'放在一個窗體上,然後設置它的'BackColor',然後關閉然後重新打開窗體,這個屬性持久。 –

+0

是的,你是對的...現在我必須檢查我的所有項目重新啓動按鈕,他們是很多... 感謝您的回答,它顯着幫助 – Robouste