【问题标题】:"Binding" a property to some logic将属性“绑定”到某些逻辑
【发布时间】: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); }
    }
}

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

【问题讨论】:

  • 为什么 TicketDetails 不作为 Ticket 的属性存在?这样,您只需与 Ticket 进行交互,并使用它来管理对各种值的更改。维护 Ticket 的集合似乎更合理,因为(没有看到更多问题)Ticket 和 TicketDetails 之间似乎存在牢固的“属于”关系,您可以在这里建模。
  • @dash 在 Henk 的回答中查看 cmets。另外,如果您觉得这样更容易解决,请随时根据您建议的设计回答我的问题。

标签: c# .net wpf binding inotifypropertychanged


【解决方案1】:

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

【讨论】:

  • 这会让我知道何时添加/删除了物品,但是当物品价格发生变化时,我仍然需要寻求 INotifyPropertyChanged 的​​帮助,对吧?
  • 没错。您还必须附加到所有新添加的票务详细信息的属性更改事件。
  • Sheesh,我坐下来评论 30 分钟,Henk 试图让他告诉我,如果我只为他更改我的设计(因为他的回答清楚地表明了这一点),我的问题将如何更容易解决最后说不管我的设计如何,这仍然是答案。如果一个拥有 120k 代表的人说这是答案,那么我想我也会。
  • @BrandonMoore - 你得到的答案通常与问题一样好和详细。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-18
  • 2010-10-28
  • 1970-01-01
  • 2011-03-03
  • 2011-03-10
  • 1970-01-01
  • 2021-01-23
相关资源
最近更新 更多