【问题标题】:Unable to Display Nested treeView Items无法显示嵌套的 treeView 项
【发布时间】:2015-12-07 14:10:56
【问题描述】:

在我的视图模型中,我有以下结构:

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>

理想情况下,我应该看到类似于下图的树结构,但只是将命名空间值作为根,仅此而已。

编辑

进行了以下更改,但仍然只获得顶级元素:

 <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}"/>

【问题讨论】:

    标签: c# wpf xaml treeview


    【解决方案1】:

    如果您能包含所有代码,那将非常有帮助。我只能猜测很多没有显示的东西。您未包含的代码中可能还有其他问题。

    但首先,您似乎在模板中将错误的东西绑定到 ItemsSource。您省略了类定义,但看起来 Level1Child 将其子项保存在名为 Level2Child 的属性中。在 Level1Child 的模板中,DataContext 是 Level1Child 的实例,而不是视图模型的实例。所以绑定到 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>
    

    在运行时 Visual Studio 的“输出”窗格中,当它尝试解析该绑定时,您将获得很多关于 DataContext 实际是什么以及绑定试图解决自身问题的信息。

    跟踪输出会减慢速度,因此不要将其永久保留在那里。解决问题后将其取出。

    【讨论】:

    • @Simsons 太好了。是我想的吗?
    • 是的,将源设置在错误的位置,Trace 提供了帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-06
    • 1970-01-01
    • 1970-01-01
    • 2018-07-05
    • 2018-08-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多