0

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?

3
  • What is your XAML binding to? Commented May 13, 2011 at 13:38
  • listBox1.ItemSource = DataModel.Entities.Peoples; Commented May 13, 2011 at 13:43
  • There is no XAML binding. In listbox uses overriden People.ToString() and all work correct Commented May 13, 2011 at 13:43

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

No, it's not observable collection, but maybe inherits from ObservableCollection<>. But why listbox refreshes after deleting element and not refreshing after inserting.
That's a good point I bet Entity Framework doesn't automatically refresh on Add (but has to on remove so it doesn't cause weird errors). Try telling it to Refresh your Peoples and see if it solves it for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.