Why does the Browsable attribute make a property unconnected?

I am trying to use System.Windows.Forms.PropertyGrid.

To make a property not visible in this grid should be used BrowsableAttributefor false. But adding this attribute makes the property unconnected.

Example: Create a new Windows Forms project and leave TextBoxit PropertyGridon Form1. Using the following code, the width is TextBoxnot tied to Data.Width:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        Data data = new Data();
        data.Text = "qwe";
        data.Width = 500;

        BindingSource bindingSource = new BindingSource();
        bindingSource.Add(data);

        textBox1.DataBindings.Add("Text", bindingSource, "Text", true,
            DataSourceUpdateMode.OnPropertyChanged);
        textBox1.DataBindings.Add("Width", bindingSource, "Width", true,
            DataSourceUpdateMode.OnPropertyChanged);

        propertyGrid1.SelectedObject = data;
    }
}

Code for the data class:

public class Data : IBindableComponent
{
    public event EventHandler TextChanged;
    private string _Text;
    [Browsable(true)]
    public string Text
    {
        get
        {
            return _Text;
        }
        set
        {
            _Text = value;
            if (TextChanged != null)
                TextChanged(this, EventArgs.Empty);
        }
    }

    public event EventHandler WidthChanged;
    private int _Width;
    [Browsable(false)]
    public int Width
    {
        get
        {
            return _Width;
        }
        set
        {
            _Width = value;
            if (WidthChanged != null)
                WidthChanged(this, EventArgs.Empty);
        }
    }

    #region IBindableComponent Members

    private BindingContext _BindingContext;
    public BindingContext BindingContext
    {
        get
        {
            if (_BindingContext == null)
                _BindingContext = new BindingContext();

            return _BindingContext;
        }
        set
        {
            _BindingContext = value;
        }
    }

    private ControlBindingsCollection _DataBindings;
    public ControlBindingsCollection DataBindings
    {
        get 
        {
            if (_DataBindings == null)
                _DataBindings = new ControlBindingsCollection(this);

            return _DataBindings;    
        }
    }

    #endregion

    #region IComponent Members

    public event EventHandler Disposed;

    public System.ComponentModel.ISite Site
    {
        get
        {
            return null;
        }
        set
        {

        }
    }

    #endregion

    #region IDisposable Members

    public void Dispose()
    {
        throw new NotImplementedException();
    }

    #endregion
}

If you switch the Browsable attribute to true for each property in Data, it will work. Now it seems that BindingSource is looking for a data source with a viewable attribute.

+5
2

:

Reflector , "" ListBindingHelper, CurrencyManager, , , BindingSource ( System.Windows.Forms namespace).

, CurrencyManager ListBindingSource.GetListItemProperties. GetListItemPropertiesByInstance ( ). :

return TypeDescriptor.GetProperties(target, BrowsableAttributeList);

BrowsableAttributeList:

private static Attribute[] BrowsableAttributeList
{
    get
    {
        if (browsableAttribute == null)
        {
            browsableAttribute = new Attribute[] { new BrowsableAttribute(true) };
        }
        return browsableAttribute;
    }
}

, , , BrowsableAttribute(true). , BindableAttribute, , , BrowsableAttribute .

, , BrowsableAttribute. - , , TypeConverter, , : PropertyGrid.

+6

BrowsableAttribute , . , - [Browsable(false)].

PropertyGrid, ( ) TypeConverter, ICustomTypeDescriptor, TypeDescriptionProvider - - , , PropertyGrid , , do (.BrowsableAttributes) .

, , .

TypeConverter, PropertyGrid, ; -, , - Attribute.IsDefined :

using System.Windows.Forms;
using System;
using System.Linq;
using System.ComponentModel;
static class Program
{
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Data data = new Data { Name = "the name", Value = "the value" };
        using (Form form = new Form
        {
            Controls =
            {
                new PropertyGrid {
                    Dock = DockStyle.Fill,
                    SelectedObject = data
                },
                new TextBox {
                    Dock = DockStyle.Bottom,
                    DataBindings = { {"Text", data, "Value"}, }
                },
                new TextBox {
                    Dock = DockStyle.Bottom,
                    DataBindings = { {"Text", data, "Name"}, }
                }
            }
        })
        {
            Application.Run(form);
        }        
    }
}
[TypeConverter(typeof(DataConverter))]
class Data
{
    class DataConverter : ExpandableObjectConverter
    {
        public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
        {
            var props = base.GetProperties(context, value, attributes);
            return new PropertyDescriptorCollection(
                (from PropertyDescriptor prop in props
                 where prop.Name != "Value"
                 select prop).ToArray(), true);
        }
    }
    public string Value { get; set; }
    public string Name { get; set; }
}
+3

All Articles