2012-07-20 115 views
0

我有一個從UserControl繼承的基類,其中有一個面板。我做了一個可以讓我顯示/隱藏面板的屬性。父控件的顯示/隱藏面板

public partial class BaseControl : UserControl 
{ 
    // ... 

    private Panel panTitle; // this is actually declared in the designer file.. 

    public BaseControl() 
    { 
     InitializeComponent(); 

     // hide the panel by default 
     IsTitlePanelVisible = false; 
    } 

    [DefaultValue(false)] 
    public bool IsTitlePanelVisible 
    { 
     get { return panTitle.Visible; } 
     set { panTitle.Visible = value; } 
    } 
} 

現在,如果我打開一些其他控件從設計師的BaseControl繼承,面板是可見的! 在屬性窗口中將IsTitlePanelVisible屬性更改爲true並返回false後,它會消失。

我還將面板本身的Visible屬性設置爲在BaseControl的設計器中爲false,但它仍然顯示出來。

有沒有人有關於如何讓面板在設計器中打開派生控件時不顯示的建議?

編輯:爲了使事情更清楚,有以下幾點: 我已經有了相當多的派生控件,我不想全部改變它們。 如果我打開一個派生的控件並手動將該值設置爲false,一切工作正常,但我不明白爲什麼它不會工作,因爲該值在基本控件的構造函數中設置爲false ..

+1

爲什麼在該類中聲明「panTitle」,而不是在designer.cs文件中聲明? – 2012-07-20 15:19:37

+0

我只在這裏添加它來表明它是Panel類型的,它實際上是在設計器中聲明的。 – loreggia 2012-08-22 14:19:12

回答

0

有您嘗試將panTitle.Visible設置爲默認設置爲false?

的ComponentModel DefaultValue屬性僅用於設計者確定PropertyGrid中的屬性是否應該被顯示爲粗體(髒),以及是否它的值應該產生的InitializeComponent方法導出BaseControl

+0

這隻會在運行時隱藏。至於現在,我看到的唯一可能性是在衍生控件的設計器代碼中將該屬性設置爲false,即使它應該已經爲假。 – loreggia 2012-08-22 15:20:04

1

我做了一個快速測試應用程序,看看我是否可以複製你的問題。我做的唯一不同的是在設計師中添加面板,並將其可見性設置爲false。它正確地做到了這一點。它看起來像你正在手動創建panTitle面板。何時/何時將它添加到您的控件中,您最好的選擇就是像上面所述添加面板。


編輯:

在閱讀你的問題有點接近,好像你不想要的面板,當你在看DerivedUserControl的設計選項卡showup。我發佈的內容不會改變,我不確定這種行爲是否可以改變。當你將它放在窗體上時它將不可見,並且以這種方式按預期行事。


這是一個快速工作的例子。

Form1中

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace WindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
    { 
     DerivedUserControl dv = new DerivedUserControl(); 

     public Form1() 
     { 
      InitializeComponent(); 

      this.Controls.Add(dv); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      if (dv.IsTitlePanelVisible) 
       dv.IsTitlePanelVisible = false; 
      else 
       dv.IsTitlePanelVisible = true; 
     } 
    } 
} 

基地用戶控件

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Drawing; 
using System.Data; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace WindowsFormsApplication1 
{ 
    public partial class BaseControl : UserControl 
    { 
     public BaseControl() 
     { 
      InitializeComponent(); 
     } 

     [DefaultValue(false)] 
     public bool IsTitlePanelVisible 
     { 
      get { return panTitle.Visible; } 
      set { panTitle.Visible = value; } 
     } 

    } 
} 

BaseControl.Designer。CS的InitializeComponent

private void InitializeComponent() 
{ 
    this.panTitle = new System.Windows.Forms.Panel(); 
    this.SuspendLayout(); 
    // 
    // panTitle 
    // 
    this.panTitle.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(192)))), ((int)(((byte)(255))))); 
    this.panTitle.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; 
    this.panTitle.Location = new System.Drawing.Point(0, 0); 
    this.panTitle.Name = "panTitle"; 
    this.panTitle.Size = new System.Drawing.Size(150, 147); 
    this.panTitle.TabIndex = 0; 
    this.panTitle.Visible = false; 
    // 
    // BaseControl 
    // 
    this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F); 
    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 
    this.Controls.Add(this.panTitle); 
    this.Name = "BaseControl"; 
    this.ResumeLayout(false); 

} 

派生的用戶控件

using System.Drawing; 
using System.Data; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace WindowsFormsApplication1 
{ 
    public partial class DerivedUserControl : BaseControl 
    { 
     public DerivedUserControl() 
     { 
      InitializeComponent(); 
     } 
    } 
} 
+0

「在更接近地閱讀您的問題時,您好像不是希望在查看DerivedUserControl的設計選項卡時顯示面板。「這正是我一直試圖解決的問題。 – loreggia 2012-08-22 14:22:01

+0

我會編輯問題以使其更清晰。 – loreggia 2012-08-22 14:55:56

2

也許你需要調用基類的構造

class DerivedControl : BaseControl 
{ 
    public DerivedControl() 
     : base() 
    { 

    } 
} 

class BaseControl : UserControl 
{ 
    public BaseControl() 
    { 
     InitializeComponent(); // makes the panel visible by default 
     IsTitlePanelVisible = false // makes the panel hidden explicity 
    } 
} 

同樣來自MSDN

DefaultValueAttribute不會使用屬性值初始化成員爲自動 。您必須在代碼中設置初始值 。

+0

沒有改變。另外,如果我沒有弄錯,base()調用默認由編譯器插入。 – loreggia 2012-08-22 14:53:25