Artur Carvalho
Artur Carvalho

Reputation: 7167

WPF databinding: database or objects?

I'm trying to create this:

Tag1 has the ordered list of objects: O1, O3, O2. Tag2 has the ordered list of objects: O1, O4.

Every time I click a tag, I want to see the list of objects. So clicking Tag1 would show in a listbox:

But I would like to keep the auto update so every time I edit or add/delete an object it auto updates (I suppose I need to implement something like the interfaces INotifyCollectionChanged and INotifyPropertyChanged?).

I could use a database and have the tables Tag, Object and TagObject, this last one with the TagID and ObjectID. But I also would like to avoid databases, since it's a desktop application.

I could also use objects like ObservableCollections, but I have the problem of having duplicate objects. I can use references to the objects but it gets messy.

Anyone has any suggestion on how to do this?

Thank you.

Upvotes: 1

Views: 1348

Answers (2)

Jimmy McNulty
Jimmy McNulty

Reputation: 370

Move all the logic controlling the updated data out of the WPF page and into another class that pushes the new data into the WPF page, when it changes, rather than the WPF pulling the data out of the objects.

Here is some example code:

class WpfPage
{
   public List OrderedListForTag1 { set { /* whatever GUI binding code you need to deal with the new list for tag 1 */ }


   public List OrderedListForTag2 { set { /* whatever GUI binding code you need to deal with the new list for tag 2*/ }

}

class WpfPresenter
{
  WpfPage thePage;

  public void Tag1Selected()
  {
      //Calculate changes to 01, 02, 04 etcetce
      //If changed, update the page
      thePage.OrderedListForTag1 = //new list of objects
  }
}

This is one of the model-view-controller pattern(s) which is very common in GUI construction. This series of articles covers the concepts.

Upvotes: 1

John
John

Reputation: 30518

One option would be to create an object that contains a dataset (System.Data namespace). Inside the dataset it would have 3 tables that would be linked using defined foreign keys the same as if it was in a database. These can later be stored to XML if need be.

Your object would then have to expose a dataview which can be set as the datacontext and bound too.

Editing the dataset in code then updates the screen and editing the screen will update the dataset.

Upvotes: 1

Related Questions