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 に戻すと、表示されなくなります。

また、BaseControl のデザイナでパネル自体の Visible プロパティを false に設定しましたが、それでも表示されます。

デザイナーで派生コントロールを開いたときにパネルが表示されないようにする方法について誰かアドバイスはありますか?

編集: より明確にするために、次の追加があります: 既に非常に多くの派生コントロールがあり、それらすべてを変更したくありません。派生コントロールを開いて値を手動で false に設定すると、すべて正常に動作しますが、ベース コントロールのコンストラクターで値が false に設定されているため、なぜ動作しないのか理解できません。

4

3 に答える 3

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 によって、メンバーが属性の値で自動的に初期化されることはありません。コードで初期値を設定する必要があります。

于 2012-07-20T15:18:44.617 に答える
1

あなたの問題を再現できるかどうかを確認するために、簡単なテスト アプリケーションを作成しました。私が別の方法で行った唯一のことは、デザイナーにパネルを追加し、その可視性を false に設定することでした。それを行うと正しく機能しました。panTitle Panel を手動で作成しているようです。コントロールにいつ/どこに追加するのか、上で述べたようにパネルを追加するのが最善の策です。


編集:

質問をもう少し詳しく読むと、DerivedUserControl の [デザイン] タブを見ているときにパネルを表示したくないようです。私が投稿したことはそれを変更しません。その動作を変更できるかどうかはわかりません。ただし、フォームにドロップしても表示されず、期待どおりに動作します。


これは簡単な作業例です。

フォーム1

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();
        }
    }
}
于 2012-07-20T15:17:06.603 に答える
0

panTitle.Visibleデフォルトでfalseに設定しようとしましたか?

ComponentModelDefaultValue属性は、デザイナーが propertyGrid のプロパティを太字 (ダーティ) で表示するかどうか、およびその値を派生クラスのInitializeComponentメソッドに生成するかどうかを決定するためにのみ使用されます。BaseControl

于 2012-07-20T15:15:44.450 に答える