gpt4 book ai didi

c# - 为什么 Browsable 属性使属性不可绑定(bind)?

转载 作者:太空狗 更新时间:2023-10-29 22:35:11 25 4
gpt4 key购买 nike

我正在尝试使用 System.Windows.Forms.PropertyGrid

要使属性在此网格中不可见,应将 BrowsableAttribute 设置为 false。但是添加此属性会使该属性不可绑定(bind)。

示例:创建一个新的 Windows 窗体项目,并将 TextBoxPropertyGrid 拖放到 Form1 上。使用下面的代码,TextBox 的宽度不会绑定(bind)到 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;
}
}

数据类的代码是:

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
}

如果您将 Data 中每个属性的 Browsable 属性切换为 true,它就会起作用。现在好像 BindingSource 通过 Browsable 属性搜索数据源。

最佳答案

基于更新示例的更新答案:

我在 Reflector 中做了一些挖掘,发现“问题”实际上在 ListBindingHelper 中,它被 CurrencyManager 使用,而 CurrencyManager 又被BindingSource(这些都在 System.Windows.Forms 命名空间中)。

为了发现可绑定(bind)属性,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,您将无法解决这个问题。您唯一的选择是按照 Marc 的建议并使用自定义 TypeConverter,或者使用此问题中的解决方案之一过滤属性网格本身:Programatically Hide Field in PropertyGrid .

关于c# - 为什么 Browsable 属性使属性不可绑定(bind)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2051337/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com