Page Navigation in WPF
Navigating between different pages or screens is a common need when developing WPF applications. This can be achieved through a variety of methods, including MVVM (Model-View-ViewModel).
MVVM method
MVVM is a design pattern that separates the logic of an application into different components: models, views, and view models. In this case, the view is responsible for displaying the UI, the model represents the underlying data, and the view model acts as an intermediary between the two, converting the model's data into a format suitable for the view.
Usage
To use MVVM to implement page navigation:
MainWindow.xaml:
Page model:
ViewModel:
MainViewModel:
SelectPageCommand:
Achievement
<code class="language-xml"><ContentControl Content="{Binding SelectedPage}"></ContentControl></code>
<code class="language-csharp">public ICommand SelectPageCommand => new RelayCommand(SelectPage); ... public void SelectPage(object param) { if (param is PageName pageName && Pages.TryGetValue(pageName, out IPage selectedPage)) { SelectedPage = selectedPage; } }</code>
This approach provides a concise and flexible way to navigate pages, making it easy to add or remove pages and reducing code duplication.
The above is the detailed content of How to Implement Page Navigation in WPF Using the MVVM Pattern?. For more information, please follow other related articles on the PHP Chinese website!