4

我有一個實體框架模型更新實體框架時,在模型的不同屬性更改屬性

public partial class Product 
{ 
    public Product() 
    { 
     this.Designs = new HashSet<Design>(); 
    } 

    public int BookingProductId { get; set; } 
    public System.Guid BookingId { get; set; } 
    public decimal Price { get; set; } 

    public virtual Booking Booking { get; set; } 
    public virtual ICollection<Design> Designs { get; set; } 
} 

...在這我想更新以響應新設計的價格屬性被添加到產品。我試圖以此爲榜樣:

How can I change an Entity Framework ICollection to be an ObservableCollection?

所以我的課的內容如下:

public partial class Product : INotifyPropertyChanged 
{ 
    public Product() 
    { 
     this.Designs = new ObservableCollection<Design>(); 
    } 

    public int BookingProductId { get; set; } 
    public System.Guid BookingId { get; set; } 
    public decimal Price { get; set; } 

    public virtual Booking Booking { get; set; } 
    public virtual ObservableCollection<Design> Designs { get; set; } 

    public event PropertyChangedEventHandler PropertyChanged; 
    protected void NotifyPropertyChanged([CallerMemberName] string propertyName = "") 
    { 
     var handler = PropertyChanged; 
     if (handler != null) 
     { 
      handler(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 
} 

,但如果我添加一個新設計的產品作爲這樣

product.Designs.Add(new Design()); 

那麼NotifyPropertyChanged方法永遠不會被觸發。有誰知道爲什麼?

在此先感謝

+1

基於一些回答其他問題,我不知道你應該看看'INotifyCollectionChanged'事件。 –

回答

3

NotfiyPropertyChanged只會在您設置整個集合而不是包含的項目時調用。

這是problme解決方法:

public partial class Product : INotifyPropertyChanged 
{ 
    public Product() 
    { 
     this.Designs = new ObservableCollection<Design>(); 
     this.Designs.CollectionChanged += ContentCollectionChanged; 
    } 

    public void ContentCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) 
    { 
     // This will get called when the collection is changed 
     // HERE YOU CAN ALSO FIRE THE PROPERTY CHANGED 
    } 

    public int BookingProductId { get; set; } 
    public System.Guid BookingId { get; set; } 
    public decimal Price { get; set; } 

    public virtual Booking Booking { get; set; } 
    public virtual ObservableCollection<Design> Designs { get; set; } 

    public event PropertyChangedEventHandler PropertyChanged; 
    protected void NotifyPropertyChanged([CallerMemberName] string propertyName = "") 
    { 
     var handler = PropertyChanged; 
     if (handler != null) 
     { 
      handler(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 
} 
+0

太好了,非常感謝。我現在也注意到,當我從數據庫中檢索產品對象時,會觸發ContentCollectionChanged事件,並且包含設計,這是有道理的,但是我想知道是否有任何方法可以區分這種情況.Add(new Design( )); ??? – ledragon

+0

通常情況下,您不必這麼做,假設您使用的是MVVM模式,並且您已將您的集合綁定到ListBox。 ListBox將觸發此事件,例如SelectedItem(s)已更改等 –

+0

不知道我明白...我可以重述嗎?我已經從DB中檢索了一個Product和相關設計:var product = this.GetDbSet ()。Include(c => c.BookingDesigns).Single(c => c.BookingProductId == bookingProductId);此時每次將產品添加到產品時,都會觸發ContentCollectionChanged。然後,我希望添加一個新的設計爲... product.Designs.Add(new Design());並且ContentCollectionChanged也被觸發。在這一點上,我希望對產品的價格屬性執行操作,而不是從數據庫中檢索。那有意義嗎? – ledragon

3

NotifyPropertyChanged永遠不會觸發因爲加入一個新元素的設計集合實際上並不改變任何產品的屬性。如果要跟蹤集合本身內的更改,則應該實現INotifyCollectionChanged接口。 ObservableCollection已經實現了,所以你可以簡單地處理CollectionChanged事件。

相關問題