2015-12-07 76 views
1

在我的視圖模型我有以下結構:無法顯示嵌套的TreeView項目

class MainWindowViewModel:BaseEntity 
{ 

    #region Output Proprties 

    public ObservableCollection<Customer> Customers { get; private set; } 

    public ObservableCollection<TreeViewItems> RoorTreeViewItem { get; set; } 

    public ObservableCollection<Level2Child> l2Childs { get; set; } 

    public ObservableCollection<Level1Child> l1Childs { get; set; } 

    public Level1Child l1Child { get; set; } 


    #endregion 


    public MainWindowViewModel() 
    { 
     ClickCommand = new RelayCommand(paremer => 
      { 
       var customeList = SampleMVVM.Service.Service.GetAllCustomers(); 
       Customers = new ObservableCollection<Customer>(customeList); 


       ObservableCollection<TreeViewItems> tViewIte = new ObservableCollection<TreeViewItems>(); 

       l1Childs = new ObservableCollection<Level1Child>(); 


       l2Childs = new ObservableCollection<Level2Child>(); 

       Level2Child l2Child1 = new Level2Child { Name = "Zems001", Description = "Zemms as ZemsBond" }; 
       Level2Child l2Child2 = new Level2Child { Name = "Zems002", Description = "Zemms as ZemsBond" }; 
       Level2Child l2Child3 = new Level2Child { Name = "Zems003", Description = "Zemms as ZemsBond" }; 
       Level2Child l2Child4 = new Level2Child { Name = "Zems004", Description = "Zemms as ZemsBond" }; 
       Level2Child l2Child5 = new Level2Child { Name = "Zems005", Description = "Zemms as ZemsBond" }; 

       l2Childs.Add(l2Child1); 
       l2Childs.Add(l2Child2); 
       l2Childs.Add(l2Child3); 
       l2Childs.Add(l2Child4); 
       l2Childs.Add(l2Child5); 




       Level1Child l1Child = new Level1Child { Name = "Bond", Description = "Gems Bond", Level2Child = l2Childs }; 

       l1Childs.Add(l1Child); 

       TreeViewItems rootItem = new TreeViewItems {Name= "Shon Conery",Description= "Octopussy", Level1Child = l1Childs }; 

       tViewIte.Add(rootItem); 

       RoorTreeViewItem = new ObservableCollection<TreeViewItems>(tViewIte); 

       NotifyPropertyChanged("Customers"); 
       NotifyPropertyChanged("RoorTreeViewItem"); 

      }); 
    } 

    #region InputCommands 

    public ICommand ClickCommand { get; private set; } 

    #endregion 



} 

我試圖用XAML來顯示樹形視圖:

<Window x:Class="SampleMVVM.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase" 
    xmlns:vm="clr-namespace:SampleMVVM.ViewModel" 
    Title="MainWindow" Height="350" Width="525"> 
<Window.Resources> 
    <!--<HierarchicalDataTemplate DataType = "{x:Type vm:Level1Child}" ItemsSource = "{Binding Path=l1Childs}"> 
     <TextBlock Text="{Binding Name}"></TextBlock> 
    </HierarchicalDataTemplate> 
    <HierarchicalDataTemplate DataType = "{x:Type vm:Level2Child}" ItemsSource = "{Binding Path=l2Childs}"> 
     <TextBlock Text="{Binding Name}"></TextBlock> 
    </HierarchicalDataTemplate>--> 
    <HierarchicalDataTemplate 
     DataType="{x:Type vm:Level1Child}" 
     ItemsSource="{Binding Path=Level2Child, diag:PresentationTraceSources.TraceLevel=High}"> 
      <TextBlock Text="{Binding Name}"></TextBlock> 
    </HierarchicalDataTemplate> 
</Window.Resources> 
<Grid> 

    <TreeView Name="viewsTreeView" ItemsSource="{Binding RoortTreeViewItem}"> 

    </TreeView> 
    <Button Height="50" Width="100" Content="Get Records" Command="{Binding ClickCommand}" Margin="132,260,285,10"/> 
</Grid> 

理想我應該看到類似於下圖的樹結構,只是以root身份獲取名稱空間值,沒有別的。

編輯

形成如下的變化,但仍然只獲得頂級元素:

<Window.Resources> 

     <HierarchicalDataTemplate 
      DataType="{x:Type vm:TreeViewItems}" 
      ItemsSource="{Binding Path=l2Childs, diag:PresentationTraceSources.TraceLevel=High}"> 
       <TextBlock Text="{Binding Name}"></TextBlock> 
     </HierarchicalDataTemplate> 
    </Window.Resources> 
    <Grid> 

     <TreeView Name="viewsTreeView" ItemsSource="{Binding RootTreeViewItem}"/> 

enter image description here

回答

1

這將是非常有益的,如果你將包括所有的代碼。我只是猜測了很多沒有顯示的東西。代碼中可能還有些問題沒有包括在內。

但是,首先,你看起來你是錯誤的東西綁定到你的模板中的ItemsSource。您忽略了您的類定義,但它看起來像Level1Child將其子代保存在名爲Level2Child的屬性中。在Level1Child的模板中,DataContext是Level1Child的一個實例,而不是您的viewmodel的一個實例。所以綁定到屬性上Level1Child:

<HierarchicalDataTemplate 
    DataType="{x:Type vm:Level1Child}" 
    ItemsSource="{Binding Path=Level2Child}" 
    > 
    <TextBlock Text="{Binding Name}"></TextBlock> 
</HierarchicalDataTemplate> 

另外,你有沒有模板在所有的根項目類型,TreeViewItems。您還需要該類型的模板,與其他類型一樣,您需要將ItemsSource綁定到TreeViewItems類上實際的子集合屬性的正確名稱。

您可以更輕鬆地診斷這些綁定問題,方法是將跟蹤添加到不符合您期望的綁定。 System.Diagnostics命名空間添加到最外層標籤在你的XAML文件:

<Window 
    ... 
    xmlns:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase" 
    ... 
    > 

並添加此附加屬性的結合,這不是您正常工作:

diag:PresentationTraceSources.TraceLevel=High 

...就像這樣:

<HierarchicalDataTemplate 
    DataType="{x:Type vm:Level1Child}" 
    ItemsSource="{Binding Path=Level2Child, diag:PresentationTraceSources.TraceLevel=High}" 
    > 
     <TextBlock Text="{Binding Name}"></TextBlock> 
</HierarchicalDataTemplate> 

在Visual Studio在運行時你的「輸出」窗格中,當它試圖解決結合,你會得到很多信息有關的DataContext什麼是真正的,和什麼結合爲t試圖去解決自己。

跟蹤輸出會減慢速度,所以不要永久保留它。當你解決問題時把它拿出來。

+0

Got it resolved Now – Simsons

+0

@Simsons太棒了。這是我的想法嗎? –

+0

是的,在錯誤的地方設置了來源,Trace有幫助。 – Simsons