I am new to MVVM and Prism. I have the below ViewModel
using System.Collections.Generic;
using BasicMVVMQuickstart_Desktop.Model;
using Microsoft.Practices.Prism.Mvvm;
namespace BasicMVVMQuickstart_Desktop.ViewModels
{
public class QuestionnaireViewModel : BindableBase
{
private Questionnaire questionnaire;
public QuestionnaireViewModel()
{
this.Questionnaire = new Questionnaire();
this.AllColors = new[] { "Red", "Blue", "Green" };
}
public Questionnaire Questionnaire
{
get { return this.questionnaire; }
set {
SetProperty(ref this.questionnaire, value);
}
}
public IEnumerable<string> AllColors { get; private set; }
}
}
I am trying to set Questionnaire from another viewmodel and update the WPF UI with the set value from another viewmodel. But unfortunately it does not work. Below is the code for the other viewmodel. I set the property in " private void OnSubmit(object obj)", but when I run application and press the submit, the UI does not get update. Anyone now what else needs to be added for this work? If I set "this.QuestionnaireViewModel.Questionnaire.Age = 32;" inside "public MainWindowViewModel()" , then the UI gets updated at runtime but with OnSubmit does not work. Please help.
using System.Diagnostics;
using System.Text;
using System.Windows.Input;
using BasicMVVMQuickstart_Desktop.Model;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.Mvvm;
namespace BasicMVVMQuickstart_Desktop.ViewModels
{
public class MainWindowViewModel : BindableBase
{
public MainWindowViewModel()
{
this.SubmitCommand = new DelegateCommand<object>(this.OnSubmit);
this.QuestionnaireViewModel = new QuestionnaireViewModel();
this.ResetCommand = new DelegateCommand(this.OnReset);
}
public ICommand SubmitCommand { get; private set; }
public ICommand ResetCommand { get; private set; }
public QuestionnaireViewModel QuestionnaireViewModel { get; set; }
private void OnSubmit(object obj)
{
Debug.WriteLine(this.BuildResultString());
this.QuestionnaireViewModel.Questionnaire.Age = 32;
}
private void OnReset()
{
this.QuestionnaireViewModel.Questionnaire = new Questionnaire();
}
private string BuildResultString()
{
StringBuilder builder = new StringBuilder();
builder.Append("Name: ");
builder.Append(this.QuestionnaireViewModel.Questionnaire.Name);
builder.Append("\nAge: ");
builder.Append(this.QuestionnaireViewModel.Questionnaire.Age);
builder.Append("\nQuestion 1: ");
builder.Append(this.QuestionnaireViewModel.Questionnaire.Quest);
builder.Append("\nQuestion 2: ");
builder.Append(this.QuestionnaireViewModel.Questionnaire.FavoriteColor);
return builder.ToString();
}
}
}