【问题标题】:Binding a TreeView to a graph of EF Entities [duplicate]将 TreeView 绑定到 EF 实体图 [重复]
【发布时间】:2016-09-07 21:18:46
【问题描述】:

我正在做一个小项目来学习学习 WPF MVVM。一切都很好,直到我遇到了这个障碍:我尝试将树视图绑定到可观察的集合以显示部门及其教师。数据被急切地加载,所以没有延迟加载。

这是我的视图模型:

public class NavigationTreeViewModel : BindableBase
{
    private CV.Model.Context.CVContext _dbContext;

    public NavigationTreeViewModel()
    {
        _dbContext = new Model.Context.CVContext();
        LoadDepartments();
    }

    private void LoadDepartments()
    {
        var result = _dbContext.Departments.Include(a => a.Teachers);
        _departments = new ObservableCollection<Department>(result.ToList());
    }

    private ObservableCollection<Teacher> _teachers;
    public ObservableCollection<Teacher> Teachers
    {
        get { return _teachers; }
        set { SetProperty(ref _teachers, value); }
    }

    private ObservableCollection<Department> _departments;
    public ObservableCollection<Department> Departments
    {
        get { return _departments; }
        set { SetProperty(ref _departments, value); }
    }

}

我可以设置我的绑定来显示部门名称和教师,如下所示:

<UserControl x:Class="CV.Modules.Views.NavigationTreeView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:vm="clr-namespace:CV.Modules.ViewModels"
         xmlns:prism="http://prismlibrary.com/"             
         prism:ViewModelLocator.AutoWireViewModel="True">

<UserControl.DataContext>
    <vm:NavigationTreeViewModel/>
</UserControl.DataContext>

<TreeView ItemsSource="{Binding Departments}" Margin="5">
    <TreeView.ItemTemplate>
        <HierarchicalDataTemplate ItemsSource="{Binding Teachers}">
            <TextBlock Text="{Binding Name}"/>
        </HierarchicalDataTemplate>
   </TreeView.ItemTemplate>
</TreeView>

</UserControl>

但是,如果我想在父实体中显示部门名称并在子实体中显示教师的姓氏,这将不起作用。所以我根据这个article把树视图的模板改成了控件的资源。这是我所做的:

<UserControl.Resources>
    <DataTemplate x:Key="TeacherLevel">
        <TextBlock Text="{Binding LastName}"/>
    </DataTemplate>
    <HierarchicalDataTemplate x:Key="DepartmentLevel"
                              ItemsSource="{Binding Departments}"
                              ItemTemplate="{StaticResource TeacherLevel}">
        <TextBlock Text="{Binding Name}"/>
    </HierarchicalDataTemplate>
</UserControl.Resources>

<TreeView ItemTemplate="{StaticResource DepartmentLevel}"
          ItemsSource="{Binding Departments}"
          Margin="5"/>

这种方式只显示部门名称,而不显示教师的姓氏。 我注意到在尝试访问我的实体的属性时没有 Intellisense 可用,我唯一可用的是我的 viewModel 中的属性。在我的第一次绑定尝试中,我可以看到所有部门属性,但在我的第二次绑定尝试中看不到。

我的问题是,如何将树视图绑定到在每个级别显示不同属性的多级实体图?

我在这里遗漏了一些非常基本的东西,但我无法找到它。任何帮助将不胜感激。

谢谢

【问题讨论】:

标签: c# wpf entity-framework mvvm treeview


【解决方案1】:

感谢@Sir Rufo 提供链接。

我的模板中缺少的是我绑定到的数据类型。需要 HierarchicalDataTemplate 中的 DataType 才能知道我绑定到哪个属性。

两种建议的解决方案都定义了要绑定到 ViewModel 中的树视图的实体。

我的模型是在不同的程序集中定义的,所以我最终在我的视图中引用了我的模型。这样我的视图就知道我的模型,我很确定我这样做是在打破 MVVM 模式。我不喜欢创建额外的相同类来为我的视图提供数据绑定的想法。

我的代码现在的样子:

<UserControl x:Class="CV.Modules.Views.NavigationTreeView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:vm="clr-namespace:CV.Modules.ViewModels"
         xmlns:data="clr-namespace:CV.Model;assembly=CV.Model"
         xmlns:prism="http://prismlibrary.com/"             
         prism:ViewModelLocator.AutoWireViewModel="True">

<UserControl.Resources>

    <HierarchicalDataTemplate ItemsSource="{Binding Teachers}"
                              DataType="{x:Type data:Department}">
        <TextBlock Text="{Binding Name}"/>
    </HierarchicalDataTemplate>

    <DataTemplate DataType="{x:Type data:Teacher}">
        <TextBlock Text="{Binding LastName}" />
    </DataTemplate>
</UserControl.Resources>

<TreeView ItemsSource="{Binding Departments}" Margin="5"/>
</UserControl>

我将来自 SO 的链接标记为答案,因为它指出了如何回答我的问题。 当我找到一个不会用我的模型实体污染视图的解决方案时,我将在未来编辑这个答案。 干杯

【讨论】:

    猜你喜欢
    • 2016-08-10
    • 1970-01-01
    • 1970-01-01
    • 2012-04-06
    • 1970-01-01
    • 1970-01-01
    • 2018-03-12
    • 1970-01-01
    • 2016-06-17
    相关资源
    最近更新 更多