Best way to bind WCF DomainService data to a TreeView in Silverlight?

592 views Asked by At

I've got a database structure similar to this:

Items

  • ID
  • Name
  • ParentItemFK (tasks & notes can have any item as a parent; categories can only have other categories as a parent)
  • CategoryFK
  • TaskFK
  • NoteFK

Categories

  • ID
  • Description (optional)

Tasks

  • ID
  • FullDescription
  • IsComplete

Notes

  • ID
  • FullText

The items are to be displayed in a TreeView. An item can be either a category, task, or note (so one and only one of the FK columns are filled in).

I'd like to display all items in a TreeView. I am using a DomainService with WCF to get the data from the back-end, for display in Silverlight.

I could programmatically build objects to be displayed in the TreeView (i.e. make an object such as ItemNode, with child ItemNodes for each sub-item). This would probably work alright, but when the data that the DomainService has is refreshed, I think the whole list of ItemNodes would have to be rebuilt (which could happen often).

I'm using entity framework underneath the DomainService, if that matters.

Is there a better way to do this??

Thanks!

1

There are 1 answers

0
Evan Larsen On BEST ANSWER

I did something similar to this and I created a custom object which represented my hierarchy. Then I created a helper class to recurse through the custom object and create Tree nodes to bind to the TreeView.

I also used the Hierarchy SQL Datatype to represent my Hierarchy in the database.

Since your using Entity framework, I'm thinking your best bet is to just create some TreeNode class which will take in your entities and then recurse through them and spit back out a Hierarchy of TreeNodes which you can then bind to the TreeView.

Also, you will want to put the unique identifier for that entity in the TreeNode.Value so that when a user interacts with a node, you will be able to easily modify the entity which represents the TreeNode. So, create another class which will recurse through your entities and get a reference to that entity based on its identifier.

hth