I am trying to pass a User between two ViewModels. To do this I am using the MVVM light Messenger framework. Even though a breakpoint is hit where I try to action the model that is passed to the second ViewModel, some TextBlocks that are bound to properties are not updated.
What I have tried:
Here is how I pass the 'User' when someone clicks a login button:
#region Login
private DelegateCommand _loginCommand;
public ICommand LoginCommand
{
get
{
_loginCommand = new DelegateCommand(param => Login());
return _loginCommand;
}
}
private async void Login()
{
var waitWindow = new PleaseWaitWindow();
waitWindow.Show();
if (await LoginCheck())
{
Messenger.Default.Send(new ChangePage(typeof(DashboardViewModel)));
Messenger.Default.Send(_chosenUser);
}
waitWindow.Close();
}
#endregion
Here is how I subscribe to that message and use its contents:
public DashboardViewModel()
{
Messenger.Default.Register<User>(this, OnUserReceived);
Messenger.Default.Register<ChangePage>(this, ChangePage);
}
private void OnUserReceived(User user)
{
FirstName = user.FirstName;
LastName = user.LastName;
}
And the two TextBlocks that are bound to the FirstName and LastName properties..:
<StackPanel VerticalAlignment="Center" Margin="5">
<TextBlock Text="{Binding FirstName}" Foreground="White" Margin="5" FontSize="18"/>
<TextBlock Text="{Binding LastName}" Foreground="White" FontWeight="Bold" Margin="5" FontSize="20"/>
</StackPanel>
If I set a breakpoint where I action the Messenger then FirstName and LastName are set to the correct values. However, the TextBlocks are not updated and their text remains null.
I have implemented a test button on the DashboardView that simply sets FirstName ="Test"; and this does update the TextBlock to say Text so I am confused as to why passing in a type of User and using its FirstName and LastName doesn't work.
EDIT:
private void OnUserReceived(User user)
{
Application.Current.Dispatcher.Invoke(() =>
{
FirstName = user.FirstName;
LastName = user.LastName;
});
}