WPF Data binding: How to organize projects and classes in a solution?

…衆ロ難τιáo~ 提交于 2019-12-13 05:23:11

问题


I have the following projects/assemblies in my solution:

  1. Entities; It is a class library that contains two classes: Account and AccountDetail. Account class has a property Details which is of type ObservableCollection<AccountDetail> and I use it to store the details of the account object.
  2. 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 the Account objects (alongside the AccountDetail collection within them).
  3. 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
  4. Gui.Controller; It is a class library that contains one class: AccountWindowController which is supposed to be the "bridge" between the AccountController from the Core assembly and the AccountsWindow from the Gui 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:

  1. Where should I placed that list of accounts, in the AccountWindowController or somewhere else?
  2. Should that list be of a type ObservableCollection?
  3. Do I need that list of accounts at all?
  4. When data binding, should I create a Window.Resource from the Gui.Controller or Entities 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!


回答1:


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.



来源:https://stackoverflow.com/questions/4257542/wpf-data-binding-how-to-organize-projects-and-classes-in-a-solution

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!