非常によく似た多数の UserControls があります。彼らは多くの共通の行動を共有しています。私は共通のものを持つ基本クラスを使用しており、必要に応じてクラスを特殊化しています。
class BaseControl : UserControl
{
// common
}
class RedControl : BaseControl
{
// specialized
}
class BlueControl : BaseControl
{
// specialized
}
など...
これは、BaseControl に含まれる子コントロールのレイアウトの挿入または変更を開始する必要があるまでは、かなりうまく機能します。たとえば、RedControl は、ベース コントロールの特定のパネルに Button を追加する必要があります。それ以外の場合は、他の基本子コントロールのサイズまたはレイアウトを変更する必要があります。
次のコードを試したところ、実行時にボタンが表示されませんでした...
public partial class RedControl : BaseControl
{
public RedControl()
{
InitializeComponent();
addButtonToBase(); // no button shows up
this.PerformLayout();
}
void addButtonToBase()
{
Button button = new Button();
button.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)));
button.Location = new System.Drawing.Point(3, 3);
button.Size = new System.Drawing.Size(23, 23);
button.Text = "My Button";
baseSplitContainer.Panel1.Controls.Add(button); // protected child control in base
}
// ...
}
addButtonToBase() を仮想化し、BaseControl の InitalizeComponent() で生成されたコードに手動で追加すると、ボタンをbaseSplitContainer の子として表示できます。BaseControl のレイアウトはまだ続いており、C#.Net のコンストラクターで仮想関数を呼び出すことが許可されています....
したがって、機能しても、良い解決策ではありません。1 つには、VS デザイナーで BaseControl を編集すると、IntializeComponent 内の addBaseControl() の呼び出しが削除されます。別の場合、コンストラクターでの仮想関数の呼び出しは危険に感じます。
基本コントロールのレイアウトを派生コントロールで再び発生させる必要があると思います...試してみましたが、間違っていたか、機能しません...
ところではい、私はWPFがこれに適していることを知っています。他のシステムの制約により使用できません。