WPF Data binding: How to organize projects and cla

2019-09-03 16:48发布

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条回答
我命由我不由天
2楼-- · 2019-09-03 17:06

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.

查看更多
登录 后发表回答