Visual Studios 2005-デザイナー/プロパティウィンドウのカスタムプロパティをクリア
-
22-07-2019 - |
質問
午前中、
画像プロパティを使用してカスタムコントロールを作成しました。その画像プロパティは、プライベート画像変数の取得/設定です。
このget / setを有効にしてデザイナーからプロパティをクリアする方法を教えてもらえますか?
つまり標準のPictureBoxに画像を追加すると、Delキーを押してPictureBoxから画像を消去できます。独自のカスタムコントロールでこの動作を複製するにはどうすればよいですか?
解決
最も単純なレベルでは、 DefaultValueAttribute
が仕事をするはずです:
private Bitmap bmp;
[DefaultValue(null)]
public Bitmap Bar {
get { return bmp; }
set { bmp = value; }
}
より複雑なシナリオでは、リセットを追加してみてください。 a>メソッド;例:
using System;
using System.Drawing;
using System.Windows.Forms;
class Foo {
private Bitmap bmp;
public Bitmap Bar {
get { return bmp; }
set { bmp = value; }
}
private void ResetBar() { bmp = null; }
private bool ShouldSerializeBar() { return bmp != null; }
}
static class Program {
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Form form = new Form();
PropertyGrid grid = new PropertyGrid();
grid.Dock = DockStyle.Fill;
grid.SelectedObject = new Foo();
form.Controls.Add(grid);
Application.Run(form);
}
}
所属していません StackOverflow