WPF MVVM navigation: seamless switching view
You are developing a WPF application containing multiple views using MVVM. You want seamless navigation between views without creating a separate window.
Solution:
Different from certain data suggestions, MVVM navigation can be achieved without using the external framework. The following is a simplified solution:
Create view model:
Define a basic view model class (BaseViewModel), which contains public attributes and InotifyPropertyChanged.In your sub -view XAML: (here you need to supplement specific command binding examples, such as using Button and Command)
(Note: The above XAML examples need to define a ICOMMAND attribute called NavigateTopersonCommand in MainWindowViewModel, and set the ViewModel property in its execution method as PersonViewModel)
<code class="language-csharp">public BaseViewModel ViewModel { get; set; }</code>
Through this method, you can achieve simple and efficient WPF MVVM navigation without relying on any external navigation framework. Remember, you need to implement the corresponding commands and attributes in ViewModel to complete the navigation logic.
<code class="language-xml"><DataTemplate DataType="{x:Type ViewModels:MainViewModel}"><MainView></MainView></DataTemplate> <DataTemplate DataType="{x:Type ViewModels:PersonViewModel}"><PersonView></PersonView></DataTemplate></code>
The above is the detailed content of How to Implement Seamless WPF MVVM Navigation Without External Frameworks?. For more information, please follow other related articles on the PHP Chinese website!