我是MVVM和Prism的新手。我有以下視圖模型MVVM Prism WPF - 設置公共屬性和SetProperty
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; }
}
}
我試圖從另一個視圖模型設置調查問卷,並與另一視圖模型設定值更新WPF UI。但不幸的是它不起作用。以下是其他視圖模型的代碼。我將屬性設置爲「private void OnSubmit(object obj)」,但是當我運行應用程序並按下提交時,UI不會更新。現在任何人都需要爲這項工作添加什麼?如果我設置「this.QuestionnaireViewModel.Questionnaire.Age = 32;」內部「公共MainWindowViewModel()」,然後UI在運行時更新,但與OnSubmit不起作用。請幫忙。
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();
}
}
}
看來您的問題與PRISM無關。用數據綁定顯示相關的XAML標記。 – dymanoid