C# &引用;“有约束力”;某种逻辑的性质

C# &引用;“有约束力”;某种逻辑的性质,c#,.net,wpf,binding,inotifypropertychanged,C#,.net,Wpf,Binding,Inotifypropertychanged,我很确定,当我看到这个问题的答案时,我会说“duh”,但我的思维并没有我现在想要的那么好,所以我正在这里旋转一个异步帮助线程来帮助我 假设以下类别: public class DerivedTicket: Ticket { public ObservableCollection<TicketDetail> TicketDetails { get; set; } } public class Ticket { public static readonly Depend

我很确定,当我看到这个问题的答案时,我会说“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); }
    }
}
公共类衍生票证:票证
{
公共ObservableCollection TicketDetails{get;set;}
}
公务舱票
{
public static readonly dependencProperty SubTotalProperty=dependencProperty.Register(“SubTotal”、typeof(decimal)、typeof(TicketsRow));
public static readonly dependencProperty TaxProperty=dependencProperty.Register(“Tax”、typeof(decimal)、typeof(TicketsRow));
public static readonly dependencProperty TotalProperty=dependencProperty.Register(“总计”、typeof(十进制)、typeof(TicketsRow));
公共小数小计
{
获取{return(decimal)this.GetValue(SubTotalProperty);}
set{this.SetValue(SubTotalProperty,value);}
}
公共十进制税
{
获取{返回(十进制)this.GetValue(TaxProperty);}
set{this.SetValue(TaxProperty,value);}
}
公共十进制总数
{
获取{return(decimal)this.GetValue(TotalProperty);}
set{this.SetValue(TotalProperty,value);}
}
}
公共类票证详情
{
public static readonly dependencProperty ItemIdProperty=dependencProperty.Register(“ItemId”,typeof(int?),typeof(TicketDetailsRow));
public static readonly dependencProperty PriceProperty=dependencProperty.Register(“Price”、typeof(decimal)、typeof(TicketDetailsRow));
public static readonly dependencProperty TaxProperty=dependencProperty.Register(“Tax”,typeof(decimal?),typeof(TicketDetailsRow));
public static readonly dependencProperty Description=dependencProperty.Register(“Description”、typeof(string)、typeof(TicketDetailsRow));
公共int?ItemId
{
获取{return(int?)this.GetValue(ItemIdProperty);}
设置{this.SetValue(ItemIdProperty,value);}
}
[现场]
公共十进制价格
{
获取{返回(十进制)this.GetValue(PriceProperty);}
set{this.SetValue(PriceProperty,value);}
}
[现场]
公共十进位税
{
获取{return(decimal?)this.GetValue(TaxProperty);}
set{this.SetValue(TaxProperty,value);}
}
[现场]
公共字符串描述
{
获取{return(string)this.GetValue(DescriptionProperty);}
set{this.SetValue(DescriptionProperty,value);}
}
}

您将如何使
小计
TicketDetails
之和保持同步?使用绑定,或在INotifyPropertyChanged的帮助下,或者其他方式?

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

,该属性将在添加/删除项目时通知我,但是我仍然需要得到INotifyPropertyChanged的帮助,当物品价格发生变化时,对吗?没错。你还必须附上所有新增加的门票细节的物业变更事件。Sheesh,我和Henk坐在一起评论了30分钟,试图让他告诉我,如果我改变我的设计,我的问题将更容易解决(因为他的回答清楚地表明了这一点)只有他最终说,不管我的设计如何,这仍然是答案。如果一个拥有120k代表的家伙说这是答案,那么我想我也会这么说。@BrandonMoore-你得到的答案通常会和问题一样好,一样详细。为什么Ticket细节不作为Ticket的属性存在?这样,您就可以与Ticket交互,并使用它来管理对各种值的更改。维护一个票证集合似乎更为合理,因为(在没有看到更多问题的情况下)票证和票证细节之间似乎存在着一种牢固的“属于”关系,您可以在这里对其进行建模。@dash请参阅对Henk答案的评论。另外,如果您觉得这样更容易解决,请根据您建议的设计回答我的问题。