在我的視圖模型我有以下結構:無法顯示嵌套的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}"/>
Got it resolved Now – Simsons
@Simsons太棒了。這是我的想法嗎? –
是的,在錯誤的地方設置了來源,Trace有幫助。 – Simsons