当我看到答案时,我很确定我会说'呃',但我的思维并不像我现在那样高效,所以我在这里开了一个异步帮助线程帮助我...
假设以下类:
public class DerivedTicket: Ticket
{
public ObservableCollection<TicketDetail> TicketDetails { get; set; }
}
public class Ticket
{
public static readonly DependencyProperty SubTotalProperty = DependencyProperty.Register("SubTotal", typeof(decimal), typeof(TicketsRow));
public static readonly DependencyProperty TaxProperty = DependencyProperty.Register("Tax", typeof(decimal), typeof(TicketsRow));
public static readonly DependencyProperty TotalProperty = DependencyProperty.Register("Total", typeof(decimal), typeof(TicketsRow));
public decimal SubTotal
{
get { return (decimal)this.GetValue(SubTotalProperty); }
set { this.SetValue(SubTotalProperty, value); }
}
public decimal Tax
{
get { return (decimal)this.GetValue(TaxProperty); }
set { this.SetValue(TaxProperty, value); }
}
public decimal Total
{
get { return (decimal)this.GetValue(TotalProperty); }
set { this.SetValue(TotalProperty, value); }
}
}
public class TicketDetail
{
public static readonly DependencyProperty ItemIdProperty = DependencyProperty.Register("ItemId", typeof(int?), typeof(TicketDetailsRow));
public static readonly DependencyProperty PriceProperty = DependencyProperty.Register("Price", typeof(decimal), typeof(TicketDetailsRow));
public static readonly DependencyProperty TaxProperty = DependencyProperty.Register("Tax", typeof(decimal?), typeof(TicketDetailsRow));
public static readonly DependencyProperty DescriptionProperty = DependencyProperty.Register("Description", typeof(string), typeof(TicketDetailsRow));
public int? ItemId
{
get { return (int?)this.GetValue(ItemIdProperty); }
set { this.SetValue(ItemIdProperty, value); }
}
[Field]
public decimal Price
{
get { return (decimal)this.GetValue(PriceProperty); }
set { this.SetValue(PriceProperty, value); }
}
[Field]
public decimal? Tax
{
get { return (decimal?)this.GetValue(TaxProperty); }
set { this.SetValue(TaxProperty, value); }
}
[Field]
public string Description
{
get { return (string)this.GetValue(DescriptionProperty); }
set { this.SetValue(DescriptionProperty, value); }
}
}
您如何保持SubTotal
与TicketDetails
的总和保持同步?使用绑定,还是借助INotifyPropertyChanged
或其他方式?
答案 0 :(得分:3)
您可以使用NotifyCollectionChanged
的事件ObservableCollection
通知故障单详细信息的更改,然后重新计算依赖项属性SubTotal
。