0
votes

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();
        }
    }
}
1
It seems that your issue has nothing to do with PRISM. Show your relevant XAML markup with data bindings.dymanoid

1 Answers

2
votes

When you change Age property in your code:

this.QuestionnaireViewModel.Questionnaire.Age = 32;

You change property of Questionnaire but not property of QuestionnaireViewModel. So, you need to inherit your Questionnaire class from BindableBase and create similar code for Age property:

private int _age;
public int Age
    {
        get { return _age; }
        set { SetProperty(ref _age, value); }
    }