UltraWinGrid 嵌套对象属性与 BindingSource 绑定
我正在开发一个 winforms 应用程序,通过 ultrawingrid 渲染域/对象数据。我正在使用绑定源将对象绑定到网格。对于简单的对象,这效果很好。
我想要解决的问题是渲染一个带有嵌套对象的对象,例如 Person 类将具有 Address 类的属性。我想将地址(街道、城市、国家/地区)的属性与 Person 类的属性一起显示为网格上的列。
网格必须是可编辑的,并且任何用户更改都需要反映在域对象上(我通过绑定源执行此操作)。
解决这个问题的最佳方法是什么?
I am working on a winforms application where I am rendering domain/object data via an ultrawingrid. I am using a bindingsource to bind the object to the grid.For simple objects this works quite well.
The thing I'm trying to get my head around is rendering an object with nested objects for e.g a Person class will have a property of Address class. I would like to display the properties of Address (Street, City, Country) as columns on the grid along with the properties of the Person class.
The grid has to be editable and any user changes need to reflect back on the domain object (which I'm doing via the binding source).
What's the best way to go about this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
绑定
我通常使用这样的代码:
绑定列表将为您处理行的添加/删除,但它不知道
Person
内部的属性。要使绑定的该部分正常工作,您需要让Person
实现 INotifyPropertyChanged。当属性发生更改时,这将通知 Ultragrid。代码将如下所示(是的,不幸的是,这使得您无法使用自动实现的属性):扁平化对象层次结构
它看起来像您所要求的 不可能直接实现。有几个选项:
Person
类以公开Address
的属性,并使用一些传递代码来处理属性的设置。(如果需要,我可以提供代码示例)
一对多嵌套对象
例如,如果您每个人有多个地址,则可以将它们嵌套在每个
下的可扩展部分中显示人
行。为此,在您的Person
内部,您需要有一个BindingList(Of Address)
,它也实现INotifyPropertyChanged
。不完全是你想要的,但一个选项:)警告的话
如果你正在做MVP。显然,您需要在视图和演示器中对
BindingList
具有相同的引用。另外,如果您需要重置内容,我建议您调用list.Clear()
而不是创建一个新内容。如果您在演示器中创建一个新的演示器,则会断开与UltraGrid
的连接,并且必须在视图中重新设置DataSource
属性。Binding
I typically use some sort of code like this:
The binding list will handle the add/remove of rows for you, but it doesn't know about the properties inside
Person
. To get that part of the binding to work, you'll need to havePerson
implement INotifyPropertyChanged. This will notify the ultragrid when the properties have changed. The code will look something like this (yes, unfortunately this makes it so you can't use auto-implemented properties):Flattening object hierarchies
It looks like what you're ask for isn't directly possible. There are a few options:
Person
class to expose the properties ofAddress
with some pass through code to handle the setting of the properties.(I can provide a code samples if needed)
One-to-many Nested Objects
If you, for example, had multiple addresses per person, you could show them nested in an expandable section under each
Person
row. To do this, inside yourPerson
you'd have aBindingList(Of Address)
which also implementsINotifyPropertyChanged
. Not exactly what you want, but an option :)Words of caution
A few notes if you are doing MVP. You'll need to have the same reference to the
BindingList
in your view and presenter, obviously. Also, if you need to reset the contents, I'd recommend callinglist.Clear()
instead of creating a new one. If you create a new one in your presenter, you'll have broken the connection with theUltraGrid
and you'll have to re-set theDataSource
property in the view.