将属性“绑定”到某个逻辑

时间:2013-01-14 08:49:47

标签: c# .net wpf binding inotifypropertychanged

当我看到答案时,我很确定我会说'呃',但我的思维并不像我现在那样高效,所以我在这里开了一个异步帮助线程帮助我...

假设以下类:

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或其他方式?

1 个答案:

答案 0 :(得分:3)

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

相关问题