2015-09-05 35 views
0

我是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(); 
     } 
    } 
} 
+0

看來您的問題與PRISM無關。用數據綁定顯示相關的XAML標記。 – dymanoid

回答

2

當您更改代碼Age屬性:

this.QuestionnaireViewModel.Questionnaire.Age = 32; 

您更改QuestionnaireViewModel問卷的財產,但不屬性。因此,您需要從BindableBase繼承您的調查問卷類,併爲Age屬性創建類似的代碼:

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

太棒了,那正是我需要做的事情。謝謝。 – user3033921