1

我很确定当我看到这个问题的答案时我会说'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); }
    }
}

您将如何使SubTotal与 的总和保持同步TicketDetails?使用绑定,还是借助INotifyPropertyChanged,或其他方式?

4

1 回答 1

3

您可以使用事件NotifyCollectionChangedObservableCollection通知工单详细信息的更改,然后重新计算依赖属性SubTotal

于 2013-01-14T08:53:04.330 回答