Tom
Tom

Reputation: 58

Silverlight 4 MVVM TreeView Control

Im writing a Silverlight 4 App with a TreeView and Ria Services in MVVM to display customers hierarchically. I dont want to load all customers from database, I want to reload them by expanding a vertex instead. Is there a chance to do this using MVVM Pattern?

Within database's customers model, there is an "Parent_id" relationship and no "child_id"!

Thanks a lot!

Upvotes: 0

Views: 1329

Answers (1)

vortexwolf
vortexwolf

Reputation: 14037

Yes, it is possible. But be informed that it is a comlicated task.

At first, you should have a viewmodel with the following properties:

  • Id (used for receiving childitems by parentId)
  • Title (shown in the treeview)
  • ChildItems (real collection or blank items)
  • IsExpanded
  • IsBlank (if this item is child item and isn't loaded yet)

At the beginning you have a list of these models, but collection ChildTtems should consist of blank items. If childitems collection is empty - you will not be able to expand a parent item.

Next step - bind IsExpanded property. You can find solution on this link.

    private bool isExpanded;

    public bool IsExpanded
    {
        get { return isExpanded; }
        set
        {
            isExpanded = value;
            OnPropertyChanged("IsExpanded");
            if(isExpanded)
                this.UpdateChildItems();
        }
    }

    public ObservableCollection<HierarchyViewModel> ChildItems { get; set; }

    void UpdateChildItems()
    {
        //Check wheter the child items are blank (this.ChildItems.Any(ci=>ci.IsBlank))
        //and if answer is yes, receive real items from service, 
        //transform each of them to a viewmodel class and set IsBlank=false
    }

Also, you have to receive a number of childitems for each item and generate collection of blank items.

Upvotes: 1

Related Questions