問題は単純です (これが単純な解決策であることを願っています!): プロパティ「Element」(PropertyGrid オブジェクト内) がゼロのときに非表示 ( Browsable(false) ) にしたいです。
public class Question
{
...
public int Element
{
get; set;
}
}
問題は単純です (これが単純な解決策であることを願っています!): プロパティ「Element」(PropertyGrid オブジェクト内) がゼロのときに非表示 ( Browsable(false) ) にしたいです。
public class Question
{
...
public int Element
{
get; set;
}
}
PropertyGrid とカスタム コントロールでプロパティを非表示にする最も簡単な方法は次のとおりです。
public class Question
{
...
[Browsable(false)]
public int Element
{
get; set;
}
}
動的に行うには、このコードを使用できます。質問はクラスで、プロパティは要素であるため、コレクションから要素を削除せずに表示または非表示にできます。
PropertyDescriptorCollection propCollection = TypeDescriptor.GetProperties(Question.GetType());
PropertyDescriptor descriptor = propCollection["Element"];
BrowsableAttribute attrib = (BrowsableAttribute)descriptor.Attributes[typeof(BrowsableAttribute)];
FieldInfo isBrow = attrib.GetType().GetField("browsable", BindingFlags.NonPublic | BindingFlags.Instance);
//Condition to Show or Hide set here:
isBrow.SetValue(attrib, true);
propertyGrid1.Refresh(); //Remember to refresh PropertyGrid to reflect your changes
したがって、答えを絞り込むには:
public class Question
{
...
private int element;
[Browsable(false)]
public int Element
{
get { return element; }
set {
element = value;
PropertyDescriptorCollection propCollection = TypeDescriptor.GetProperties(Question.GetType());
PropertyDescriptor descriptor = propCollection["Element"];
BrowsableAttribute attrib = (BrowsableAttribute)descriptor.Attributes[typeof(BrowsableAttribute)];
FieldInfo isBrow = attrib.GetType().GetField("browsable", BindingFlags.NonPublic | BindingFlags.Instance);
if(element==0)
{
isBrow.SetValue(attrib, false);
}
else
{
isBrow.SetValue(attrib, true);
}
}
}
}
BrowsableAttributes/BrowsableProperties と HiddenAttributes/HiddenProperties を試してください: