无法显示嵌套的treeView项

时间:2015-12-07 14:10:56

标签: c# wpf xaml 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 个答案:

答案 0 :(得分: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"
    ...
    >

并将此附加属性添加到Binding,它不能按预期工作:

diag:PresentationTraceSources.TraceLevel=High

......就像这样:

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

在你的&#34;输出&#34; Visual Studio在运行时窗格,当它尝试解析该绑定时,您将获得有关DataContext实际是什么的大量信息,以及绑定尝试解决自身的问题。

跟踪输出减慢了速度,因此请不要将其永久保留在那里。当你解决问题时把它拿出来。