I’m fairly sure I’m going to say ‘duh’ when I see the answer to this, but my mind isn’t as performant as I’d like currently so I’m spinning up an asynchronous help thread here to assist me…
Assume the following classes:
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); }
}
}
How would you go about keeping the SubTotal in sync with the sum of the TicketDetails? Use a binding, or with the help of INotifyPropertyChanged, or some other way?
You could use the event
NotifyCollectionChangedof theObservableCollectionto be notified about changes of the ticket details and then recalculate the dependency propertySubTotal.