jeudi 23 octobre 2014

ViewModel properties handling


Vote count:

0




Few words about my MVVM-based WPF app. I've defined MainWindowViewModel:



public class MainWindowViewModel : ViewModelBase
{
...
public ViewModelBase CurrentViewModel {get; set;}
...
}


This is data context for my MainWindow.xaml. This property has it's own template binding to specific View (depends on which 'inner' view model is currently included). This MainWindowViewModel is listening for Mvvm-Light Messenger message which changes current view model. Now my problem.


I'd like to assign there LoginViewModel with property SelectedUser. I'm getting property of SelectedUser from another ViewModel, where I'm executing this command:



this.UserSelectedCommand = new RelayCommand(() =>
{
LoginViewModel viewModel = new LoginViewModel();
viewModel.SelectedLogin = this.SelectedUserFromList;

MessageHelper.SendViewModel(viewModel); //change CurrentViewModel
viewModel.RaisePropertyChanged("SelectedLogin");
});


My LoginViewModel.cs has defined default value of SelectedLogin in constructor.


So in ViewModel which is responsible for displaying list of users I'm selecting one, I have this value in SelectedUserFromList property and I'd like to go back to login View with selected user. Which means I'd like to assign new LoginViewModel with SelectedLogin to CurrentViewModel property form MainWindowViewModel.


After executing UserSelectedCommand I can see proper View which is binded to LoginViewModel, but I have everywhere default values of properties.


I guess that because of this



<DataTemplate DataType="{x:Type vm:LoginViewModel}">
<views:LoginView/>
</DataTemplate>


I do have proper ViewModel in CurrentViewModel (with changed SelectedLogin) but template is loading LoginViewModel again (so constructor sets default values). Am I right? Because I can't see any other reasons.



asked 7 mins ago







ViewModel properties handling

Aucun commentaire:

Enregistrer un commentaire