Update item in collection & display in WPF
I have the following scenario:
- User enters values in a form and submits
- The submit methods calls a service passing the form related object as a parameter
- The object is persisted in the db
- The object gets assigned a new ID and gets returned to the calling method
- 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
精彩评论