【发布时间】:2015-06-11 13:17:58
【问题描述】:
我有一个包含几个 TextBox 元素和一个进度条的表单。我希望在 TextBox 分配了一些值时更新进度条。
因此,当设置属性时,我尝试增加 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
【问题讨论】:
-
遇到了类似的问题,尽管我使用的是 caliburn.micro,但建议的解决方案也应该对您有所帮助 - 将第二个视图模型注册到第一个的 propertychange 事件并更新您的任何内容想要link