【发布时间】:2012-12-28 05:30:52
【问题描述】:
当我看到这个问题的答案时,我很确定我会说“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,或者其他方式?
【问题讨论】:
-
为什么 TicketDetails 不作为 Ticket 的属性存在?这样,您只需与 Ticket 进行交互,并使用它来管理对各种值的更改。维护 Ticket 的集合似乎更合理,因为(没有看到更多问题)Ticket 和 TicketDetails 之间似乎存在牢固的“属于”关系,您可以在这里建模。
-
@dash 在 Henk 的回答中查看 cmets。另外,如果您觉得这样更容易解决,请随时根据您建议的设计回答我的问题。
标签: c# .net wpf binding inotifypropertychanged