实体框架与WPF Datagrid绑定 [英] Entity Framework & WPF Datagrid binding

查看:79
本文介绍了实体框架与WPF Datagrid绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用EF和WPF的非常简单的小应用程序。我正在尝试为我们的小型团队创建一个非常轻量级的项目跟踪器。这些类如下:

I have a pretty simple little app that is using EF and WPF. I'm trying to create a very lightweight 'project tracker' for our small team at work. The classes look like:

class Project
{
    public int ProjectId { get; set; }
    public string Name { get; set; }

    public List<ProjectNote> ProjectNotes { get; set; }

    public string Status { get; set; }
    public string Description { get; set; }

    public string ProjectLead { get; set; }
}

class ProjectNote
{


    public int ProjectNoteId { get; set; }
    public DateTime DateCreated { get; set; }
    public DateTime DateEdited { get; set; }
    public string CreatedBy { get; set; }
    public string LastEditedBy { get; set; }
    public string Detail { get; set; }
    public int ProjectId { get; set; }

}

我希望能够在数据网格:

I want to be able to display the following fields in a datagrid:


  1. Project.Name

  2. Project.Description

  3. (最近使用的)ProjectNote.Detail

基本上,我希望有一个Datagrid允许用户查看最新的

Basically, I want to have a Datagrid that allows my user to see the most recent notes for a Project and edit them if need be.

我在弄清楚如何构造代码结构(我在使用Code First / POCO)时遇到了麻烦用户从Datagrid视图创建一个新的ProjectNote。我不知道我是否需要我的Project类的另一个属性来允许单个ProjectNote,或者我是否可以在用户编辑包含Project数据的Datagrid时对此进行编码(通过ViewModel)以正确地实例化新的ProjectNote

I'm having trouble figuring out how to structure my code (I'm using Code First/POCO) to allow the user to create a new ProjectNote from the Datagrid view. I don't know if I need another property of my Project class to allow for a single ProjectNote, or if I can code this (through a ViewModel) to properly instantiate a new ProjectNote when the user is editing a Datagrid that contains the Project data.

如何创建允许跨多个实体进行编辑/创建的数据网格?

How can I create a datagrid that allows editing/creation across multiple entities?

推荐答案

我建议创建一个ViewModel,也许是ProjectNoteViewModel,它包含Note和Project。然后,将ProjectNoteViewModels的集合/列表绑定到 DataGrid

I would suggest creating a ViewModel, perhaps ProjectNoteViewModel, which contains the Note as well as the Project. Then, you bind your collection/list of ProjectNoteViewModels to the DataGrid.

或者,您可以添加项目导航属性设置为ProjectNote类,这将允许您将Project.Name和Project.Description包含在ProjectNote的 DataGridRow 中。如果对此关系几乎没有或没有业务逻辑/复杂性,则可以使用这种方法,但是如果需要执行任何复杂性或业务规则,则建议使用ViewModel。

Alternatively, you could add a Project navigation property to the ProjectNote class, which would allow you to include the Project.Name and Project.Description in the DataGridRow for the ProjectNote. If there is little or no business logic/complexity to this relationship, then this approach will work, but if there is any complexity or business rules you need to enforce, I'd recommend using a ViewModel.

本文提供了很好地一起使用了 DataGrid 和MVVM。基本上,如果您将 DataGrid 绑定到某种列表,则 DataGrid 会自动创建一个新的您的对象,并在用户单击网格底部的空白行时将其插入到集合中。

This article provides a good walk through of using the DataGrid and MVVM together. Basically, if you have DataBound the DataGrid to a list of some sort, the DataGrid will automatically create a new instance of your object for you and insert it into the collection when the user clicks the empty row at the bottom of the grid.

这篇关于实体框架与WPF Datagrid绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆