WPF 数据绑定到数据网格和实体框架数据库优先

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22406581/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 11:13:15  来源:igfitidea点击:

WPF databinding to datagrid and entity framework database first

wpfvisual-studio-2010entity-frameworkdata-bindingwpfdatagrid

提问by Tom

With Database First approach i've created the entities (with entity framework) inside my wpf project so i have the edmx file.

使用数据库优先方法,我在 wpf 项目中创建了实体(使用实体框架),因此我有 edmx 文件。

From MSDN: EF generates code from your model using T4 templates. The templates shipped with Visual Studio or downloaded from the Visual Studio gallery are intended for general purpose use. This means that the entities generated from these templates have simple ICollection properties. However, when doing data binding using WPF it is desirable to use ObservableCollection for collection properties so that WPF can keep track of changes made to the collections. To this end we will to modify the templates to use ObservableCollection.

来自 MSDN:EF 使用 T4 模板从您的模型生成代码。Visual Studio 随附或从 Visual Studio 库下载的模板旨在用于一般用途。这意味着从这些模板生成的实体具有简单的 ICollection 属性。但是,在使用 WPF 进行数据绑定时,最好将 ObservableCollection 用于集合属性,以便 WPF 可以跟踪对集合所做的更改。为此,我们将修改模板以使用 ObservableCollection。

So i've followed this tutorial to change the entities to have ObservableCollection properties: http://msdn.microsoft.com/en-us/data/jj574514.aspx(Section Updating code generation for data binding)

所以我按照本教程将实体更改为具有 ObservableCollection 属性:http: //msdn.microsoft.com/en-us/data/jj574514.aspx(部分更新数据绑定的代码生成)

In the WPF view (in the xaml file) with Visual Studio i've added a DataGrid and added this code:

在带有 Visual Studio 的 WPF 视图(在 xaml 文件中)中,我添加了一个 DataGrid 并添加了以下代码:

private void Window_Loaded_1(object sender, RoutedEventArgs e)
{
   using (SIEntities siContext = new SIEntities())
   {
      var query = from p in siContext.Customers
                  select p;

      dataGrid.ItemsSource = query.ToList();
    }
}

In a first istance, to learn how insert data, i want from code insert a new customer in the database so i've this method:

首先,要了解如何插入数据,我想从代码中在数据库中插入一个新客户,所以我有这个方法:

private void Button_Click_1(object sender, RoutedEventArgs e)
{
   using (SIEntities siContext = new SIEntities())
   {
         Customer cust1 = new Customers();
         cust1.Name = "Pippo";
         cust1.City = "London";
         siContext.Customers.Add(cust1);
         siContext.SaveChanges();
         dataGrid.Items.Refresh();
    }
 }

With this code i'm able to insert a new row in the database but i don't see this new row in the datagrid.

使用此代码,我可以在数据库中插入新行,但在数据网格中看不到此新行。

in the xaml file i've the following for datagird:

在 xaml 文件中,我为 datagird 提供了以下内容:

<DataGrid x:Name="dataGrid" HorizontalAlignment="Left" Margin="43,65,0,0" VerticalAlignment="Top" Height="234" Width="423"/>

Why? The dataGrid it's not binding to the entity? How can i show the new row added in the db also in the datagrid?

为什么?dataGrid 它没有绑定到实体?如何在数据网格中也显示添加到数据库中的新行?

Thanks

谢谢

回答by Lee O.

I would change your method of using WPF to use MVVM design pattern. You can read http://msdn.microsoft.com/en-us/magazine/dd419663.aspxfor more info on this.

我会改变你使用 WPF 的方法来使用 MVVM 设计模式。您可以阅读http://msdn.microsoft.com/en-us/magazine/dd419663.aspx了解更多信息。

Your DataGrid is bound to the results of the query. With the method you are using, you'll need to requery the database and reset the ItemsSource to the returned results. So following your pattern, you'll need to make the following changes:

您的 DataGrid 绑定到查询的结果。使用您正在使用的方法,您需要重新查询数据库并将 ItemsSource 重置为返回的结果。因此,按照您的模式,您需要进行以下更改:

private void Window_Loaded_1(object sender, RoutedEventArgs e)
{
   RefreshCustomers();
}

private void RefreshCustomers()
{
   using (SIEntities siContext = new SIEntities())
   {
      var query = from p in siContext.Customers
                  select p;

      dataGrid.ItemsSource = query.ToList();
    }
}

private void Button_Click_1(object sender, RoutedEventArgs e)
{
   using (SIEntities siContext = new SIEntities())
   {
         Customer cust1 = new Customers();
         cust1.Name = "Pippo";
         cust1.City = "London";
         siContext.Customers.Add(cust1);
         siContext.SaveChanges();
    }

    RefreshCustomers();
}