简体   繁体   English

WPF MVVM-DataGrid不会将更改更新回数据库

[英]WPF MVVM - DataGrid does not update changes back to database

I have a WPF DataGrid that is bound to a ObservableCollection that is in my NinjaList ViewModel 我有一个WPF DataGrid,它绑定到NinjaList ViewModel中的ObservableCollection

public ObservableCollection<NinjaVM> Ninjas { get; set; }

And the method where Ninjas is defined 以及定义忍者的方法

public NinjaListVM()
        {
            using (var context = new NinjaApp_DatabaseEntities())
            {
                var ninjas = context.ninjas.ToList();
                Ninjas = new ObservableCollection<NinjaVM>(ninjas.Select(r => new NinjaVM(r)));
            }
        }

The code in my View is as followed 我的视图中的代码如下

<Window 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:NinjaApp_V2.Views"
        xmlns:ViewModel="clr-namespace:NinjaApp_V2.ViewModel" xmlns:NinjaApp_V2="clr-namespace:NinjaApp_V2" x:Name="NinjaCRUDWindow" x:Class="NinjaApp_V2.Views.NinjaCRUD"
        mc:Ignorable="d"
        Title="NinjaCRUD" Height="300" Width="300"
        DataContext="{Binding Ninjas, Source={StaticResource Locator}}" Loaded="onLoad">


    <Grid Margin="0,10,3.6,0.4">
        <DataGrid x:Name="DataGridNinjas" ItemsSource="{Binding Ninjas, Mode=TwoWay}" SelectedValue="{Binding Ninjas, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" AutoGenerateColumns="True">
        </DataGrid>
        <Button x:Name="btnNewNinja" Command="{Binding ShowNewNinja}"  Margin="10,228,103.4,-0.4" Content="New Ninja"/>
    </Grid>
</Window>

The view does show all the contents of the Ninja table into the Grid. 该视图确实将“忍者”表的所有内容显示到网格中。 Also when changes are made they persist throughout the time that the application is running. 同样,进行更改后,它们会在应用程序运行的整个过程中持续存在。 But changes are not being saved to the database. 但是更改未保存到数据库中。 My understanding is that if you bind to a ObservableCollection like this using Mode = TwoWay that it should automatically update the changes back to the database. 我的理解是,如果使用Mode = TwoWay这样绑定到ObservableCollection,它将自动将更改更新回数据库。 But clearly I am missing something, can somebody perhaps point out what I am doing wrong? 但是显然我缺少了什么,有人可以指出我做错了吗?

I am using MvvM Light as MvvM framework. 我正在使用MvvM Light作为MvvM框架。

Yeah, the reason is that your Viewmodels haven't a direct access to the Database. 是的,原因是您的Viewmodels无法直接访问数据库。

        //opening the database
        using (var context = new NinjaApp_DatabaseEntities()) 
        {
           //query the database and store the result in memory
            var ninjas = context.ninjas.ToList();

           //Viewmodel mapping
            Ninjas = new ObservableCollection<NinjaVM>(ninjas.Select(r => new NinjaVM(r)));
        } // in the end of the using statement the database will be "closed"

Manipulating an ninja doesn't affect the database. 操作忍者不会影响数据库。

I suspect you are using the entity framework. 我怀疑您正在使用实体框架。

so if you want to store the changes, you must reopen the Database, search for the specific ninja and overwrite the property and use the SaveChanges Methode. 因此,如果要存储更改,则必须重新打开数据库,搜索特定的忍者并覆盖该属性,然后使用SaveChanges Methode。

For practicing you can do this in the setter 为了练习,您可以在二传手中进行

    public class NinjaVM
{

    private int _id;

    private string _name;
    public string Name
    {
        get { return _name; }
        set
        { 
            using (var context = new NinjaApp_DatabaseEntities())
            {
                var ninja = context.ninjas.FirstOrDefault(n => n.Id == _id  ));
                if(ninja == null)
                return;
                ninja.Name = value;
                context.SaveChanges();
            }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM