WPF Binding on Entity Framework
I开发者_开发百科 have MS SQL Server database with one table - Peoples. From these database generated Entity Framework Data model. In WPF application I have ListBox with ItemSource = DataModel.Entities.Peoples, and two buttons - add and remove People in database. Add button:
DataModel.Entities.AddPeople(new People("test"));
DataModel.Entities.SaveChanges();
And removing:
DataModel.Entities.Remove((People)listBox1.SelectedItem);
DataModel.Entities.SaveChanges();
When I click remove button - corresponding People row removing from databases and listBox1 refreshing. But when I click add button - People added in database (see in MS SQL Enterprise Manager), but listbox does not refreshing.
How to refresh listBox on adding? Guess I forgot to set any option in DataModel?
Unless DataModel.Entities.Peoples
is an ObservableCollection
, it won't be aware of changes.
I recommend you use the MVVM pattern for this, which solves that problem perfectly.
精彩评论