2015-06-11 127 views
0

設置屬性的視圖模型從另一個視圖模型我有一個包含一些文本框元素和一個進度條形式如何。我希望當TextBox有一些值被賦值時更新進度條。我在MVVM光工具包

因此,當一個屬性設置,我嘗試增加ProgressPercent,但不實際工作。我錯過了什麼,爲什麼我不能從另一個ViewModel訪問ProgressPercent

MainViewModel.cs

public class MainViewModel : ViewModelBase 
{ 
    private int progressPercent { get; set; } 

    public MainViewModel() 
    { 
    } 

    public int ProgressPercent 
    { 
     get 
     { 
      return this.progressPercent; 
     } 
     set 
     { 
      this.progressPercent = value; 
      this.RaisePropertyChanged(() => this.ProgressPercent); 
     } 
    } 
} 

FooterViewModel

public class FooterViewModel : ViewModelBase 
{ 
    private string firstName { get; set; } 
    private string lastName { get; set; } 

    public FooterViewModel() 
    { 
    } 

    public string FirstName 
    { 
     get 
     { 
      return this.firstName; 
     } 
     set 
     { 
      this.firstName = value; 
      this.RaisePropertyChanged(() => this.FirstName); 
      Window.Current.Dispatcher.RunAsync(CoreDispatcherPriority.Low,() => 
      { 
       ProgressPercent += 10; 
      }); 
     } 
    } 

    public string LastName 
    { 
     get 
     { 
      return this.lastName; 
     } 
     set 
     { 
      this.lastName = value; 
      this.RaisePropertyChanged(() => this.LastName); 
      Window.Current.Dispatcher.RunAsync(CoreDispatcherPriority.Low,() => 
      { 
       ProgressPercent += 10; 
      }); 
     } 
    } 
} 

HeaderViewModel.cs

public class HeaderViewModel : ViewModelBase 
{ 
    private string address { get; set; } 
    private int age { get; set; } 

    public HeaderViewModel() 
    { 
    } 

    public string Address 
    { 
     get 
     { 
      return this.address; 
     } 
     set 
     { 
      this.address = value; 
      this.RaisePropertyChanged(() => this.Address); 
      Window.Current.Dispatcher.RunAsync(CoreDispatcherPriority.Low,() => 
      { 
       ProgressPercent += 10; 
      }); 
     } 
    } 

    public int Age 
    { 
     get 
     { 
      return this.age; 
     } 
     set 
     { 
      this.age = value; 
      this.RaisePropertyChanged(() => this.Age); 
      Window.Current.Dispatcher.RunAsync(CoreDispatcherPriority.Low,() => 
      { 
       ProgressPercent += 10; 
      }); 
     } 
    } 
} 

問題Accessing a property in one ViewModel from another是沒有什麼不同,但解決的辦法有沒有爲我工作,出於某種原因

其實答案就在這裏 Accessing Properties in other ViewModels in MVVM Light

+0

也有類似的問題,雖然我用caliburn.micro建議的解決方案應該對你有所幫助,以及 - 報名參加第二視圖模型的PropertyChange事件上任何你想要[鏈接]第一個和更新( http://stackoverflow.com/questions/25285020/caliburn-micro-notifying-a-viewmodel-on-property-change-in-another-viewmodel) –

回答

0

你通常必須做一些類型的通信基礎設施之間交談的的ViewModels。您是否正在使用任何類型的Mediator或MVVM框架,其中內置腳手架?

另外,您可以設置一個類,所有的人都可以訪問,並可能引發一個事件,其他的ViewModels被訂閱的屬性。

+0

我使用MVVM光工具包 –