【发布时间】:2017-07-25 22:05:39
【问题描述】:
我们有一个包含子集合的父类。这两个类都支持使用 Template10 进行更改通知。更改通知适用于每个类(即当我们更新同一类中的属性时),如下所示,但我们无法从子级触发父级更改通知。
public class ParentViewModel : ViewModelBase
{
public string ParentString { get }
public decimal? Net { get { return Total / (1 + TaxRate / 100); } }
public decimal? Tax { get { return Total - Net; } }
decimal? _Total = default(decimal?);
public decimal? Total
{
get
{
return _Total;
}
set
{
Set(ref _Total, value);
RaisePropertyChanged(nameof(Net));
RaisePropertyChanged(nameof(Tax));
}
}
public ObservableCollection<ChildViewModel> MyChildren { get; set; }
我们发现我们可以在ParentViewModel 中使用RaisePropertyChanged 来开火
Net { get { return Total / (1 + TaxRate / 100); } }
和
Tax { get { return Total - Net; } }
在ChildViewModel 我们有ChildString。我们希望将 ChildString 的更改通知 ParentString。
public class ChildViewModel : ViewModelBase
{
ParentViewModel MyParent { get; set; }
string _ChildString = default(string);
public string ChildString
{
get
{
return _ChildString;
}
set
{
Set(ref _ChildString, value);
RaisePropertyChanged(nameof(this.MyParent.ParentString));
}
}
但是ParentString 没有更新。当ChildString 更新时,我们如何强制ParentString 更新?
【问题讨论】:
-
this.MyParent分配在哪里?也许this so question 有帮助 -
@Lei Yang MyParent 由创建 ChildViewModel 实例的任何方法分配。谢谢你的链接。接受的答案对我们有用。
标签: c# mvvm uwp template10