为什么可浏览属性使属性不可绑定?

2023-12-11

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

要使属性在此网格中不可见,应使用BrowsableAttribute set to false。 但添加此属性会使该属性不可绑定。

Example:创建一个新的 Windows 窗体项目,然后删除TextBox and PropertyGrid onto Form1。使用下面的代码,宽度TextBox不受约束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,这又被使用BindingSource(这些都在System.Windows.Forms命名空间)。

要发现可绑定属性,CurrencyManager调用ListBindingSource.GetListItemProperties。在幕后,这呼吁GetListItemPropertiesByInstance(当您传入单个对象时)。该方法末尾有以下代码行:

return TypeDescriptor.GetProperties(target, BrowsableAttributeList);

并实施BrowsableAttributeList is:

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

您可以看到,实际上,它是通过以下方式过滤属性的:BrowsableAttribute(true)。它可能应该使用BindableAttribute相反,但我的猜测是,设计师意识到每个人都已经依赖于BrowsableAttribute并决定用那个来代替。

因此,不幸的是,如果您使用,您将无法解决这个问题BrowsableAttribute。您唯一的选择是要么按照 Marc 的建议进行操作,要么使用自定义的TypeConverter,或使用此问题中的解决方案之一过滤属性网格本身:以编程方式隐藏 PropertyGrid 中的字段.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为什么可浏览属性使属性不可绑定? 的相关文章

随机推荐

  • JApplet 和 KeyListener

    我正在开发一个应用程序我的计算机科学课 任务是编写一个计算器 但不使用JTextFields or JTextAreas 我想出了一个实施的想法KeyListener which 在 appletviewer 和JFrame但在 Googl
  • Vista 中的 Eclipse/adb 错误消息“无法解析 adb 版本的输出”

    我想学习Android开发 所以我下载了Eclipse Galileo和Android SDK 但是 每当我启动 Eclipse 时 都会收到错误消息 无法解析 adb 版本的输出 在控制台 DDMS 窗格中 调试输出显示 2010 06
  • iPhone Objective-C:键盘有时不会通过 resignFirstResponder 隐藏

    我有一个 UITextView 当按下返回键时 我将其称为 resignFirstResponder 文本视图does辞职第一响应者 文本框中闪烁的光标消失 但键盘有时不会消失 什么可能导致这个问题 谢谢你 声明UITextViewDele
  • 双向链表上的快速排序

    我想在同步双向链表上实现快速排序算法 我给函数 分区 左右边界 然后它开始在左侧搜索较低的值 并将较大的值放在右侧 这是有效的 因为我的枢轴元素始终是最右边的元素 并且在这一步之后它位于中间 我总是陷入无限循环 我不知道为什么 也许中止条件
  • 使 QTableView 的行随着编辑器高度的增加而扩展

    这直接来自这个问题 这是一个 MRE class MainWindow QtWidgets QMainWindow def init self super init self setWindowTitle Get a grip of tab
  • Python 中“可哈希”是什么意思?

    Python 代码中的对象到底是什么意思hashable 来自Python术语表 如果一个对象有一个在其生命周期内永不改变的哈希值 它需要一个 hash 方法 并且可以与其他对象进行比较 它需要一个 eq or cmp 方法 比较相等的可哈
  • 在 React 中更新 forEach 循环中的状态无法正常工作

    我想在每次收到承诺时循环更新一个状态 但我的状态只展现了最后的承诺 我猜这是因为 由于设置状态是异步的 它使用先前状态更新之前可用的映射 在不使用 Promise all 的情况下如何实现这一目标 const dataList setDat
  • CouchDB 和 PouchDB 之间的过滤同步

    我目前正在考虑在我想编写的下一个应用程序中使用 CouchDB 2 和 PouchDB 7 基本上 我将在中央存储中拥有一个 CouchDB Web 客户端和移动应用程序将启动一个能够思考的 PouchDB 基本上这就像一个魅力 但是 如果
  • 为什么 python 像这样排序我的字典? [复制]

    这个问题在这里已经有答案了 这是我有的字典 propertyList id int name char 40 team int realOwner int x int y int description char 255 port bool
  • 浮点数的正则表达式?

    我正在尝试编写一个正则表达式来验证浮点数 这是我迄今为止所做的 1 9 d 0 2 d 1 1 如果满足以下条件 则该号码有效 要么是正的 要么是负的 最多 2 位数字 十位或百位 百位数字不能为0 只能是1 9 比例最大为 1 小数值可以
  • StackOverflowException 未处理

    我的代码中出现此错误 MedCareProviderLibrary dll 中发生 System StackOverflowException 类型的未处理异常 这是我的代码片段以及错误的来源 它在有错误的部分上显示一个黄色箭头 显示错误的
  • 在 Matlab 中查找 pcolor 中的轮廓/边缘

    我正在尝试制作一个遵循 像素 边缘的轮廓pcolor在 Matlab 中绘图 这可能在图片中得到最好的解释 这是我的数据图 黄色数据 data 1 和蓝色数据 data 0 之间有明显的界限 请注意 这是一个pcolor绘图 因此每个 正方
  • 从 for 循环到 Java 8 Stream 示例

    我想要一个 Java 8 的简单示例Streams去理解它 我有这个代码可以返回免费出租车 我想用使用 Java 8 流的等效代码替换这个 for 循环 private List
  • PHP 根据由括号分隔的字符串中的键名称创建多维关联数组

    我有一个字符串 括号中包含可变数量的键名称 例如 str key subkey otherkey 我需要创建一个多维数组 该数组具有字符串中表示的相同键 value只是一个在这里不重要的字符串值 arr key gt subkey gt o
  • 无法导入 anaconda 中安装的包

    I have a simple question I have install resampy using anaconda conda install c conda forge resampy 现在 当我将 resampy 导入到我的
  • 采用整数并返回所有可能的加法格式的算法

    我需要编写一个算法 它接受一个整数并返回所有可能的加法格式 e g 如果我输入 6 它将返回以下字符串 0 6 6 1 1 1 1 1 1 6 1 1 1 1 2 6 1 1 1 3 6 1 1 4 6 1 5 6 2 1 1 1 1 6
  • 如何获取目录的父目录

    如何获取目录的父目录 File parent new File System out println Parent directory parent getParent prints Parent directory null 我有一个 M
  • 绕过 500 行限制

    我编写了一个我很满意的 Google Fusion Tables 脚本 如下 但它只在我的表中加载 500 行点 该表有 20 000 多行 这是我第一次来到这个社区 我真的很惊讶地发现了极限 有什么方法可以加载所有行吗
  • 计算 Pandas 中值的数量[重复]

    这个问题在这里已经有答案了 我有一个 Pandas 数据框 如下构建 Col1 Col2 1 A 1 B 1 B 2 A 2 A 3 A 3 Nan 对于 Col1 的每个值 我想计算 Col2 的每个值 忽略 Nan 值 并将总和放入关联
  • 为什么可浏览属性使属性不可绑定?

    我正在尝试使用System Windows Forms PropertyGrid 要使属性在此网格中不可见 应使用BrowsableAttribute set to false 但添加此属性会使该属性不可绑定 Example 创建一个新的