7

問題は単純です (これが単純な解決策であることを願っています!): プロパティ「Element」(PropertyGrid オブジェクト内) がゼロのときに非表示 ( Browsable(false) ) にしたいです。

    public class Question
    {
       ...

      public int Element
      {
        get; set;
      }
    }
4

4 に答える 4

29

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);
            }
          }
   }
}
于 2014-07-22T14:31:49.970 に答える
0

BrowsableAttributes/BrowsableProperties と HiddenAttributes/HiddenProperties を試してください:

詳細はこちら

于 2013-09-20T21:35:43.347 に答える