“Binding"某种逻辑的属性

本文关键字:属性 Binding quot | 更新日期: 2023-09-27 17:52:42

当我看到这个问题的答案时,我很确定我会说'duh',但是我的大脑不像我目前想的那样高效,所以我在这里旋转一个异步帮助线程来帮助我…

假设下列类:

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); }
    }
}

如何使SubTotalTicketDetails的和保持同步?使用绑定,或在INotifyPropertyChanged的帮助下,或其他方式?

“Binding"某种逻辑的属性

您可以使用ObservableCollection的事件NotifyCollectionChanged来通知票证详细信息的更改,然后重新计算依赖属性SubTotal