Observablecollection listbox not updating
I did not want to create a button on a page that has any kind of code-behind to switch pages, and I didn’t like the idea of my navigation being spread out throughout all the View Models.I finally came to realize the solution was simple: I needed a View Model for the Application itself, which contained the application state, such as the Current Page.
The MVVM pattern is a close variant of the Presentation Model pattern, optimized to leverage some of the core capabilities of WPF , such as data binding, data templates, commands, and behaviors.
In the MVVM pattern, the view encapsulates the UI and any UI logic, the view model encapsulates presentation logic and state, and the model encapsulates business logic and data.
Run the project and you should see something that looks like the images below, which quickly switches the Current Page when clicking on the Navigation buttons. Once you get more comfortable with WPF, I would recommend looking into using a Messaging System, such as MVVM Light’s Messenger, or Microsoft Prism’s Event Aggregator to broadcast Change Page commands from any View Model so you wouldn’t need to find the Application View Model to execute the Change Page Command, however that’s for another day.
The Model-View-View Model (MVVM) pattern helps you to cleanly separate the business and presentation logic of your application from its user interface (UI).
The following illustration shows the three MVVM classes and their interaction.
Like with all separated presentation patterns, the key to using the MVVM pattern effectively lies in understanding the appropriate way to factor your application's code into the correct classes, and in understanding the ways in which these classes interact in various scenarios.
The Home View Model is a blank class that inherits from IPage View Model, and the Home View is just a blank User Control.
In addition, I added an s to Products View Model since it really deals with multiple products, not a single one.
Using the MVVM pattern, the UI of the application and the underlying presentation and business logic is separated into three separate classes: the view, which encapsulates the UI and UI logic; the view model, which encapsulates presentation logic and state; and the model, which encapsulates the application's business logic and data.