WPF 数据绑定:如何在解决方案中组织项目和类?
我的解决方案中有以下项目/程序集:
- 实体;它是一个类库,包含两个类:
Account
和AccountDetail
。Account
类有一个ObservableCollection
类型的属性 Details,我用它来存储帐户对象的详细信息。 - 核心;它是一个类库,包含一个类:
AccountController
,其目的是从 Sql Server 获取数据并填充Account
对象(以及其中的 AccountDetail 集合) 。 - 桂;它是一个 WPF 应用程序项目,包含一个名为:
AccountsWindow
的 WPF 表单,其目的是显示从 Sql Server Gui.Controller 检索到的所有帐户的列表 - ;它是一个类库,包含一个类:
AccountWindowController
,它应该是Core
程序集中的AccountController
和来自Gui
程序集的AccountsWindow
并协助数据绑定。 (我不确定我是否需要这个程序集。)
这就是我想要做的:
我想使用 Core
AccountController 类从 Sql Server 获取所有帐户code> assembly 并将它们放入某个列表中。然后,我想将 AccountWindow
中的列表框与该帐户列表进行数据绑定。
我的问题:
- 我应该将该帐户列表放在
AccountWindowController
或 其他地方? - 该列表应该是 ObservableCollection 类型吗?
- 我真的需要那个账户列表吗?
- 数据绑定时,我应该从
Gui.Controller
创建一个Window.Resource
还是实体
类?
我知道要阅读的文字很多,但我的问题非常简单,因为我是 WPF 的新手,任何帮助将不胜感激。谢谢!
更新:我的痛苦继续这里。干杯!
I have the following projects/assemblies in my solution:
- Entities; It is a class library that contains two classes:
Account
andAccountDetail
.Account
class has a property Details which is of typeObservableCollection<AccountDetail>
and I use it to store the details of the account object. - Core; It is a class library that contains one class:
AccountController
whose purpose is to get the data from the Sql Server and to populate theAccount
objects (alongside the AccountDetail collection within them). - Gui; It is a WPF Application project that contains one WPF Form called:
AccountsWindow
whose purpose is to present the list of all accounts retrieved from the Sql Server - Gui.Controller; It is a class library that contains one class:
AccountWindowController
which is supposed to be the "bridge" between theAccountController
from theCore
assembly and theAccountsWindow
from theGui
assembly and to assist with the data binding. (I am not sure whether I need this assembly at all.)
Here's what I wish to do:
I want to get all accounts from the Sql Server using the AccountController
class from the Core
assembly and to put them in some list. Then, I want to data bind a list box in AccountWindow
with that list of accounts.
My questions:
- Where should I placed that list of accounts, in the
AccountWindowController
or
somewhere else? - Should that list be of a type
ObservableCollection
? - Do I need that list of accounts at all?
- When data binding, should I create a
Window.Resource
from theGui.Controller
orEntities
classes?
I know this is a lot of text to read, but my questions are really simple as I am a newbie with the WPF and any help would be greatly appreciated. Thanks!
Update: My agony is continued here. Cheers!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看来您的 Gui 将成为客户端,并将引用其他 3 个程序集。 Gui.Controller 将引用 Core 和 DataEntities,而 Core 将仅引用 DataEntities。
AccountController 应该获取列表并将其返回给 Gui.Controller。如果列表是 ObservableCollection 就可以了。列表本身应该放置在 Gui 或 Gui.Controller 中,具体取决于您是否可以从 Gui 访问 Gui.Controller 的属性。
当您将 ListBox 放入要放置在 Gui 中的 Window 中时,您需要将其绑定到集合。该集合可以是 Window 的属性。或者您可以将其绑定到一个方法,该方法可以是 Gui.Controller 的一部分。这实际上取决于您想要如何组织它。
It seems your Gui will be the client and will Reference the other 3 assemblies. Gui.Controller will reference Core and DataEntities, and Core will reference only DataEntities.
The AccountController should fetch the list and return it to the Gui.Controller. It's fine if the list is ObservableCollection. The list itself should be placed in the Gui or Gui.Controller, depending if you can access the properties of Gui.Controller from Gui.
When you put the ListBox in a Window which will be placed in the Gui, you need to bind it to a collection. The collection can be a property of the Window. Or you can bind it to a method, which can be part of Gui.Controller. It really depends on how you want to organize it.