开发者

WPF Binding on Entity Framework

开发者 https://www.devze.com 2023-03-06 04:21 出处:网络
I开发者_开发百科 have MS SQL Server database with one table - Peoples. From these database generated Entity Framework Data model.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消