c# - Why Browsable attribute makes property not bindable? -
i'm trying use system.windows.forms.propertygrid
.
to make property not visible in grid 1 should use browsableattribute
set false
. adding attribute makes property not bindable.
example: create new windows forms project, , drop textbox
, propertygrid
onto form1
. using code below, width of textbox
not bound 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; } }
the code data class is:
public class data : ibindablecomponent { public event eventhandler textchanged; private string _text; [browsable(true)] public string text { { return _text; } set { _text = value; if (textchanged != null) textchanged(this, eventargs.empty); } } public event eventhandler widthchanged; private int _width; [browsable(false)] public int width { { return _width; } set { _width = value; if (widthchanged != null) widthchanged(this, eventargs.empty); } } #region ibindablecomponent members private bindingcontext _bindingcontext; public bindingcontext bindingcontext { { if (_bindingcontext == null) _bindingcontext = new bindingcontext(); return _bindingcontext; } set { _bindingcontext = value; } } private controlbindingscollection _databindings; public controlbindingscollection databindings { { if (_databindings == null) _databindings = new controlbindingscollection(this); return _databindings; } } #endregion #region icomponent members public event eventhandler disposed; public system.componentmodel.isite site { { return null; } set { } } #endregion #region idisposable members public void dispose() { throw new notimplementedexception(); } #endregion }
if switch browsable attribute true on every property in data works. seems bindingsource searches datasource browsable attribute.
updated answer based on update example:
i've done digging in reflector , discovered "problem" in listbindinghelper
, used currencymanager
, in turn used bindingsource
(these in system.windows.forms
namespace).
to discover bindable properties, currencymanager
invokes listbindingsource.getlistitemproperties
. under hood, calls out getlistitempropertiesbyinstance
(when pass in single object). method has following line of code @ end:
return typedescriptor.getproperties(target, browsableattributelist);
and implementation of browsableattributelist
is:
private static attribute[] browsableattributelist { { if (browsableattribute == null) { browsableattribute = new attribute[] { new browsableattribute(true) }; } return browsableattribute; } }
you can see is, in fact, filtering properties browsableattribute(true)
. should using bindableattribute
instead, guess designers realized depending on browsableattribute
, decided use 1 instead.
so unfortunately won't able around issue if use browsableattribute
. options either marc suggests , use custom typeconverter
, or filter property grid using 1 of solutions in question: programatically hide field in propertygrid.
Comments
Post a Comment