When do we need to use [Browsable(true)]?

前端 未结 9 1107
清歌不尽
清歌不尽 2021-01-01 13:06

When do we need to use [Browsable(true)]?

EDIT (by SLaks): He\'s asking (I assume) why one would need to pass true as the

相关标签:
9条回答
  • 2021-01-01 13:49

    Probably when you want to make damn sure no one changes it :P

    // I want to see this, dont change it to false or I'll hunt you down...
    [Browsable(true)]
    public int MyProperty {
       get {
          // Insert code here.
          return 0;
       }
       set {
          // Insert code here.
       }
    }
    
    0 讨论(0)
  • 2021-01-01 13:54

    MSDN says it all:

    Specifies whether a property or event should be displayed in a Properties window.

    For example, if you're creating a User Control, you might want to decorate non-UI-related properties with [Browsable(false)] so that they will not be available through a "Properties" window.

    Additionally, it controls which properties of an object can be seen in a PropertyGrid.

    As for why we can pass true explicitly, I believe this is due to BrowsableAttributes property of a PropertyGrid. You can set it to contain BrowsableAttribute.No, so that the property grid will display all non-browsable members.

    0 讨论(0)
  • 2021-01-01 13:55

    BrowsableAttribute Class (System.ComponentModel)

    The documentation states:

    A visual designer typically displays in the Properties window those members that either have no browsable attribute or are marked with the BrowsableAttribute constructor's browsable parameter set to true.

    [Browsable] also defaults to true.

    ...so technically, you never need [Browsable(true)] unless you want to be very explicit.

    0 讨论(0)
提交回复
热议问题