I have navigation set up with MainWindow.xaml that has a ContentControl. Content property is bound to an object property in MainViewModel.
I have a UserListView.xaml user control which has UserListViewModel.cs and a UserDetailView.xaml user control which has a UserDetailViewModel.cs class.
UserListView displays a list of users which is fetched from the database. On this screen you can click a button for each user to edit that user in the list. There is also a button to create new users. Each button takes you to the UserDetail screen where you can edit, delete, or create a new user.
In the case of creating a new user or deleting a user, when navigating back to the UserListView screen, a new UserListViewModel is instantiated, which makes a new call to the database to get the changes just made.
The problem is that the view does not reflect the state of the ViewModel. It is like the app has cached the View from when it was first instantiated.
For example if in UserListView, I click Add New User button, am taken to UserDetailView, add details for a new User, click save, then navigate back to UserListView - the newly created user is not in the view.
Note that I have debugged, and can confirm that the ViewModel has the correct data. And that I am using MVVM Light and Fody which takes care of INotifyPropertyChanged.
Is this something to do with caching of the View? I have tried added Mode=TwoWay for binding of the DataTemplates and the binding of the ContentControl with no luck.
I have done a test where I have a delete button on UserListView which is hard coded to delete a specific user from the database, then refresh the list of users from the database which is bound to the table in the view - this works fine, the row disappears. So it seems like a caching issue.
Note that I am not supplying code snippets for the UserListView and UserDetailView and their ViewModels because I do not think it is relevant.
MainViewModel.xaml
<Window x:Class="WPFApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WPFApp"
xmlns:vm="clr-namespace:App.Logic;assembly=WPFApp"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:cmd1="http://www.galasoft.ch/mvvmlight"
mc:Ignorable="d"
Title="WPF App" WindowState="Maximized"
DataContext="{Binding Main, Source={StaticResource Locator}}">
<Window.Resources>
<ResourceDictionary>
<DataTemplate DataType="{x:Type vm:UserListViewModel}">
<local:UserListView/>
</DataTemplate>
<DataTemplate DataType="{x:Type vm:UserDetailViewModel}">
<local:UserDetailView/>
</DataTemplate>
</ResourceDictionary>
</Window.Resources>
<DockPanel>
<ContentControl Content="{Binding SelectedViewModel}"></ContentControl>
</DockPanel>
MainViewModel.cs
public class MainViewModel : ViewModelBase
{
public event PropertyChangedEventHandler PropertyChanged;
public object SelectedViewModel { get; set; }
public MainViewModel()
{
SelectedViewModel = new HomeViewModel();
}
public RelayCommand HomeNavigationCommand => new RelayCommand(HomeNavigate);
private void HomeNavigate() => Navigate(new HomeViewModel());
public RelayCommand UserListNavigationCommand => new RelayCommand(UserListNavigate);
private void UserListNavigate() => Navigate(new UserListViewModel());
private void UserDetailNavigate(UserDetailUserMessage user) => Navigate(new UserDetailViewModel(user));
private void Navigate(object viewModel)
{
SelectedViewModel = viewModel;
}
}