2010-12-10 118 views
0

我正在用MVVM中的TreeView和Ria Services編寫Silverlight 4應用程序以分層顯示客戶。 我不想從數據庫加載所有客戶,我想通過展開頂點來重新加載它們。有沒有機會使用MVVM模式來做到這一點?Silverlight 4 MVVM TreeView控件

在數據庫的客戶模型中,存在「Parent_id」關係並且沒有「child_id」!

非常感謝!

回答

1

是的,這是可能的。但是要知道,這是一個相當複雜的任務。

首先,你應該有以下屬性的視圖模型:

  • ID(用於接收parentId的childitems)
  • 標題(樹形視圖所示)
  • ChildItems(真實採集或空白項目)
  • IsExpanded
  • ISBLANK(如果這個產品子項,並且還沒有加載)

開始時您有這些模型的列表,但集合ChildTtems應該由空白項組成。如果子項收集爲空,則無法擴展父項。

下一步 - 綁定IsExpanded屬性。您可以在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 
    } 

此外,你必須接受一些childitems每個項目和產生的空白項目的集合。

+0

非常感謝!但要小心,在Silverlight中,不可能在XAML中設置「IsExpanded」綁定。您必須繼承TreeView和TreeViewItem類的硬編碼綁定。 – Tom 2010-12-15 07:57:48