WPF MVVM view navigation
In the WPF MVVM application, the navigation between views is crucial. This article aims to solve the problem of navigation from one view to another, such as switching from view 1 to view 2 and follow -up views.
ViewModelloCator and Navigation
The code provided by the code focuses on the ViewModellOcator mode. However, it raises questions about its realization. The CreateMain () function mentioned in the code is not defined, and the user is confused about the mechanism of its usage and switching view.
The simplified MVVM method
In order to simplify the implementation of the MVVM, we proposed an alternative method that uses the InotifyPropertyChaanged interface and DatateMplates to bind the view to the view model. Step 1: Define the BaseViewModel class
Create a base class baseViewModel containing public attributes and commands. All view models can be extended.
Step 2: Create binding
in the app.xaml
In the App.xaml, define datatemplates to connect the view model to its respective views.<code class="language-csharp">public class BaseViewModel : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; }</code>
Step 3: Use ContentControl
Use ContentControl and bind to the ViewModel attribute to display the view model.
<code class="language-xml"><DataTemplate DataType="{x:Type ViewModels:MainViewModel}"><MainView></MainView></DataTemplate></code>
Step 4: Switch the view in MainViewModel
To switch the view, set the ViewModel attribute in MainViewModel.
<code class="language-xml"><ContentControl Content="{Binding ViewModel}"></ContentControl></code>
To enable the viewing from the sub -view for the view navigation, define the command in the mainViewModel, and visit them by binding it.
This method provides a clear and concise MVVM navigation implementation without relying on external tool packages.
<code class="language-csharp">ViewModel = new PersonViewModel();</code>
The above is the detailed content of How to Implement Simple View Navigation in WPF MVVM?. For more information, please follow other related articles on the PHP Chinese website!