简体   繁体   English

将Windows窗体控件绑定到对象的属性

[英]Binding windows form controls to an object's properties

Is it possible to use object binding to bind a windows form control to an object so that whenever the object changes the form control reflects that change. 是否可以使用对象绑定将Windows窗体控件绑定到对象,以便每当对象更改时,窗体控件都会反映该更改。

Here's what I've done so far: 到目前为止,这是我所做的:

  1. Added a data grid to the form. 向表单添加了数据网格。

  2. Added a binding reference to the class. 添加了对类的绑定引用。 This added the properties of the class as columns to the datagrid. 这将类的属性作为列添加到数据网格。

  3. set the data grid's data source to the object. 将数据网格的数据源设置为对象。

The class is updated by an external event source. 该类由外部事件源更新。 The update is working fine but nothing appears in the data grid. 更新工作正常,但数据网格中没有任何内容。

I am wondering if I missing a step or what I'm trying to do is not possible automatically meaning I have to add some code to update the grid each time the object gets updated. 我想知道是否遗漏了某个步骤,或者我想做的事情是否不可能自动进行,这意味着每次对象被更新时,我都必须添加一些代码来更新网格。 If so, what's the point of binding in the first place. 如果是这样,绑定的重点是什么。

I think the following MSDN web site should be quite helpful for you: 我认为以下MSDN网站应该对您有很大帮助:

A brief summary is that your object that keeps the data (and can be changed) should implement the INotifyPropertyChanged interface. 一个简短的摘要是,保留数据(并且可以更改)的对象应该实现INotifyPropertyChanged接口。 The interface contains a single event that you should trigger whenever your data is changed (by the external update). 该界面包含一个事件,只要您更改数据(通过外部更新),就应该触发该事件。 If you also want to support addition and removal of items, then you'll need to take a look at IBindingList . 如果您还希望支持项目的添加和删除,则需要查看IBindingList

确保您绑定的是一个类的公共属性 ,而不仅仅是字段

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM