UltraWinGrid 嵌套对象属性与 BindingSource 绑定

发布于 2024-12-26 05:14:25 字数 265 浏览 3 评论 0原文

我正在开发一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

这个俗人 2025-01-02 05:14:25

绑定

我通常使用这样的代码:

Dim persons = new BindingList(Of Person)
UltraGrid1.DataSource = persons

绑定列表将为您处理行的添加/删除,但它不知道 Person 内部的属性。要使绑定的该部分正常工作,您需要让 Person 实现 INotifyPropertyChanged。当属性发生更改时,这将通知 Ultragrid。代码将如下所示(是的,不幸的是,这使得您无法使用自动实现的属性):

Private _phoneNumber As String
Public Property PhoneNumber As String
  Get
    Return Me._phoneNumber
  End Get
  Set(ByVal value As String)
    If value <> _phoneNumber Then
      Me._phoneNumber = value
      NotifyPropertyChanged("PhoneNumber")
    End If
  End Set
End Property

扁平化对象层次结构

它看起来像您所要求的 不可能直接实现。有几个选项:

  1. 在 InitializeRow 事件期间填写的 UI 中的未绑定列
  2. 修改 Person 类以公开 Address 的属性,并使用一些传递代码来处理属性的设置。

(如果需要,我可以提供代码示例)

一对多嵌套对象

例如,如果您每个人有多个地址,则可以将它们嵌套在每个 下的可扩展部分中显示人行。为此,在您的 Person 内部,您需要有一个 BindingList(Of Address),它也实现 INotifyPropertyChanged。不完全是你想要的,但一个选项:)

警告的话

如果你正在做MVP。显然,您需要在视图和演示器中对 BindingList 具有相同的引用。另外,如果您需要重置内容,我建议您调用 list.Clear() 而不是创建一个新内容。如果您在演示器中创建一个新的演示器,则会断开与 UltraGrid 的连接,并且必须在视图中重新设置 DataSource 属性。

Binding

I typically use some sort of code like this:

Dim persons = new BindingList(Of Person)
UltraGrid1.DataSource = persons

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 have Person 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):

Private _phoneNumber As String
Public Property PhoneNumber As String
  Get
    Return Me._phoneNumber
  End Get
  Set(ByVal value As String)
    If value <> _phoneNumber Then
      Me._phoneNumber = value
      NotifyPropertyChanged("PhoneNumber")
    End If
  End Set
End Property

Flattening object hierarchies

It looks like what you're ask for isn't directly possible. There are a few options:

  1. Unbound columns in the UI that you fill in during the InitializeRow event
  2. Modify your Person class to expose the properties of Address 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 your Person you'd have a BindingList(Of Address) which also implements INotifyPropertyChanged. 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 calling list.Clear() instead of creating a new one. If you create a new one in your presenter, you'll have broken the connection with the UltraGrid and you'll have to re-set the DataSource property in the view.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文