質問者
PropertyウィンドウまたはPropertyGridの表示内容の動的変更

質問
-
いろいろ調べましたが解決できませんので皆さんにお聞きします。
分かりやすい例としては以下のようなシナリオです。
TextBoxから派生するカスタムコントロールを作ります。
このコントロールは独自のプロパティを2つ備えます。
たとえば、bool型の"数値専用"と、int型の"小数部桁数"です。
"数値専用"がfalseと設定されたとき、"小数部桁数"の指定は無意味です。そこで、これを灰色表示かまたは隠すかして、入力できないようにしたいのです。
また、"数値専用"がtrueと設定されたときは、"小数部桁数"を設定可能な状態にしたいのです。
以上のようなことは、Visual Studioのプロパティウィンドウに対して可能でしょうか?
また、PropertyGridコントロールを直接使えば可能となるでしょうか?
どなたか方法をご教示いただけると有り難いです。
よろしくお願いします。
すべての返信
-
Yamakei さんからの引用 "数値専用"がfalseと設定されたとき、"小数部桁数"の指定は無意味です。そこで、これを灰色表示かまたは隠すかして、入力できないようにしたいのです。
また、"数値専用"がtrueと設定されたときは、"小数部桁数"を設定可能な状態にしたいのです。
以上のようなことは、Visual Studioのプロパティウィンドウに対して可能でしょうか?
普通に TypeConverter を作成して GetProperties の返却リストを編集すればよいとおもいます。
[TypeConverter("XxxxTypeConverter")]public class Xxxx : TextBox{// 数値専用プロパティを変更すると PropertyGrid を再クエリさせるように指定[class=identifier>RefreshProperties ( RefreshProperties.All ) ]public bool 数値専用{get { ... }set { ... }}// 少数部桁数はデフォルトで見えないことにする(Disable にしたいなら ReadOnly で)[Browsable(false)]public int 小数部桁数{get { ... }set { ... }}}internal class XxxTypeConverter : TypeConverter{public override PropertyDescriptorCollection GetProperties(
ITypeDescriptorContext context,
Object value,
Attribute[] attributes){PropertyDescriptorCollection coll = base.GetProperties(context, value, attributes);// 数値専用が true の時、一覧に小数部桁数を加えるif ((value as Xxx).数値専用){PropertyDescriptorCollection nonBrowsableProperties =TypeDescriptor.GetProperties(value, new Attribute[] { BrowsableAttribute.No });coll.Add(nonBrowsablePropeties.Find("少数部桁数", false));}return coll;}
}
動作確認はしていないので、もしかすると挿入する PropertyDescriptor の Attributes に入っている BrowsableAttribute を入れ替えしないといけないかもしれません。(ReadOnlyAttribute の場合は、挿入処理はいらなくて、入れ替え処理が必須です)
Yamakei さんからの引用 | |
|
単純にEnabledとかVisibleでいいんじゃないでしょうか?
数値専用はEnabledよりも優先されます。
public class MyTextBox: TextBox
{
[RefreshProperties(RefreshProperties.All)]
public bool 数値専用
{
get
{
return _数値専用;
}
set
{
_数値専用 = value;
this.Enabled = value;
}
}
public int 小数部桁数
{
get
{
int intValue;
if (int.TryParse(this.Text, out intValue)) return intValue;
return 0; //無入力・異常入力
}
set { this.Text = value.ToString(); }
}
public MyTextBox()
{
InitializeComponent();
}
private bool _数値専用 = true; {
InitializeComponent();
}
[RefreshProperties(RefreshProperties.All)]
public bool 数値専用
{
get
{
return _数値専用;
}
set
{
_数値専用 = value;
this.Enabled = value;
}
}
public int 小数部桁数
{
get
{
int intValue;
if (int.TryParse(this.Text, out intValue)) return intValue;
return 0; //無入力・異常入力
}
set { this.Text = value.ToString(); }
}
}
coll.Add(pd) でbreakさせて、coll.readOnlyを確認するとtrueです。
ILで確認しましたが readOnly が true だと NotSupportedException を発生します。
public override bool GetPropertiesSupported(ITypeDescriptorContext context)
{
return true;
}
public override PropertyDescriptorCollection GetProperties(
ITypeDescriptorContext context,
Object value,
Attribute[] attributes)
{
PropertyDescriptorCollection coll = TypeDescriptor.GetProperties(value, attributes,true);
// 数値専用が true の時、一覧に小数部桁数を加える
if ((value as MyTextBox).数値専用)
{
PropertyDescriptorCollection nonBrowsableProperties =
TypeDescriptor.GetProperties(value, new Attribute[] { BrowsableAttribute.No });
PropertyDescriptor pd = nonBrowsableProperties.Find("小数部桁数", false);
coll.Add(pd);
}
return coll;
}