简体   繁体   English

wpf mvvm与实体框架的两种方式数据绑定

[英]wpf mvvm two way databinding with Entity Framework

I have classes generated by Entity Framework as follows. 我有如下由实体框架生成的类。 My requirement is to load data from the database into a List View and if any checked changes happen in the List View, store it back. 我的要求是将数据库中的数据加载到列表视图中,如果列表视图中发生任何已检查的更改,请将其存储回去。

For that purpose in my model, I have written another partial class as below that implements INotifyPropertyChanged . 为此,在我的模型中,我编写了下面的另一个实现INotifyPropertyChanged局部类。 I want to use my Entity Framework classes as models. 我想将实体框架类用作模型。

Along the same lines I have a view containing a List View for displaying Name and Location and a check box for each row to display checked state. 同样,我有一个视图,其中包含用于显示NameLocation的列表视图,以及用于显示已选中状态的每一行的复选框。 So for the checkbox, an example of the logic I have written is CheckBox IsChecked=true , mode=two way , UpdateSourceTrigger=PropertyChanged . 因此,对于复选框,我编写的逻辑示例为CheckBox IsChecked=truemode=two wayUpdateSourceTrigger=PropertyChanged I use a OnCheckedChanged event to call db.SaveChanges in my view model. 我使用OnCheckedChanged事件在视图模型中调用db.SaveChanges

db is object of type SampleDbContext . dbSampleDbContext类型的SampleDbContext But it seems the binding is not happening, ie checked changes are not stored into the database. 但是似乎绑定没有发生,即检查的更改没有存储到数据库中。

Why are checked changes not saving to the database? 为什么检查的更改未保存到数据库?

Entity Framework classes: 实体框架类:

public partial class Datagrid
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Location { get; set; }
    public Nullable<bool> IsChecked { get; set; }
}

public partial class SampleDbContext : DbContext
{
    public SampleDbContext() : base("name=SampleDbContext")
    {}

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        throw new UnintentionalCodeFirstException();
    }

    public virtual DbSet<Datagrid> Datagrids { get; set; }
}

My Custom Class in the Models: (This is a sample code may have some spelling mistakes but please ignore it) 我在模型中的自定义类:(这是示例代码,可能会有一些拼写错误,但请忽略它)

[MetaDataType(typeof(grid))]
public partial class DataGrid
{}

public class grid:INotifyPropertyChanged
{
    public Nullable<bool> IsChecked 
    {   
        get { return IsChecked; }
        set
        {
            IsChecked=value;
            OnPropertyChanged("IsChecked");
        }
    }   

    //INotifyPropertyChanged Implementation....
}

You are calling the same Property within your property setter. 您正在属性设置器中调用同一属性。 IsChecked=value. IsChecked =值。 Create a private field to store the isChecked value. 创建一个私有字段来存储isChecked值。

[MetaDataType(typeof(grid))]
public partial class DataGrid
{
}

public class grid:INotifyPropertyChanged
{
    private bool? m_IsChecked;
    public Nullable<bool> IsChecked  
    get
    {
        return m_IsChecked;
    }
    set
    {
        if(m_IsChecked != value)
        {
            m_IsChecked=value;
            OnPropertyChanged("IsChecked");
        }
    }
}

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

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