简体   繁体   中英

Update item in collection & display in WPF

I have the following scenario:

  1. User enters values in a form and submits
  2. The submit methods calls a service passing the form related object as a parameter
  3. The object is persisted in the db
  4. The object gets assigned a new ID and gets returned to the calling method
  5. The object is add to a collection in the submit method

I have an expander control that is bound to the collection using an item list and inside each list is a sub list.

What is the best practice for refreshing the view to show the updated object. What I was doing to refresh the collection was the following:

             ObservableCollection<ProjectDto> projects = new ObservableCollection<ProjectDto>();
                Projects.ForEach(projects.Add);
                Projects.Clear();
                projects.ForEach(Projects.Add); 

This makes the expander collapse as I think it is being bound to a new collection.

What is the relationship between projects (little p) and Projects (capital P) - why two lists? What does your binding look like?

EDIT

Let's assume for discussion that Projects is an ObservableCollection, and is what is bound to your UI, and that projects is either a temporary, domain model, or backend list that you use to assemble and prepare (sort, validate, etc.) DTOs for presentation. In this case, projects can just be a List, and you can every time you do Projects = new ObservableCollection(projects) your bound UI items collection should refresh.

Berryl

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.

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