我有一个包含少量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
答案 0 :(得分:0)
您通常必须使用某种类型的通信基础设施来在ViewModel之间进行通信。您是否正在使用具有此内置脚手架的任何类型的Mediator或MVVM Framework?
或者,您可以在类中设置一个属性,使其可以访问所有属性,并且可以引发其他ViewModel订阅的事件。