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
. 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. So for the checkbox, an example of the logic I have written is CheckBox IsChecked=true
, mode=two way
, UpdateSourceTrigger=PropertyChanged
. I use a OnCheckedChanged
event to call db.SaveChanges
in my view model.
db
is object of type 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. Create a private field to store the isChecked value.
[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");
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.