2016-01-11 143 views
1

我不完全確定爲什麼這不是更新。因此,在我的應用程序中,您可以使用文本框修改右側的用戶名。這些變化實時反映在左側。當您點擊'添加新許可證'按鈕時,它會創建一個新許可證並將其添加到選定的客戶。然而,列表視圖列'Lincenes'似乎沒有更新以反映客戶擁有的許可數量。作爲測試,我在我的Obs系列中放置了打印聲明可觀察的集合沒有發射setter或更新C#wpf

 private ObservableCollection<License> licenses; 
     public ObservableCollection<License> Licenses 
     { 
      get { return licenses ?? (licenses = new ObservableCollection<License>()); } 
      set 
      { 
       Console.Write("Modified"); 
       Set(ref licenses, value); 
       RaisePropertyChanged(nameof(LicensesCount)); 
      } 
     } 

但是我注意到它從來沒有打印出於某種原因。我非常難以做什麼或改變。以下是我的代碼的主要部分。無論是客戶和許可類對象是基類的inotify的...

enter image description here enter image description here

Customer.cs和License.cs類

using System; 
using System.Collections.Generic; 
using System.Collections.ObjectModel; 
using System.Security.Cryptography; 
using System.Text; 
using System.Windows.Input; 
using WpfApplication1.Helper; 

namespace WpfApplication1.Model 
{ 
    public class Customer : NotifyBase 
    { 
     private string firstName; 
     public string FirstName 
     { 
      get { return firstName; } 
      set { Set(ref firstName, value); } 
     } 

     public string LicensesCount 
     { 
      get { return this.Licenses.Count.ToString(); } 
     } 

     private ObservableCollection<License> licenses; 
     public ObservableCollection<License> Licenses 
     { 
      get { return licenses ?? (licenses = new ObservableCollection<License>()); } 
      set 
      { 
       Console.Write("Modified"); 
       Set(ref licenses, value); 
       RaisePropertyChanged(nameof(LicensesCount)); 
      } 
     } 

     public Customer(string firstname, string lastname, string email, string company) 
     { 
      this.FirstName = firstname; 
     } 

     // Commands 
     private ICommand addNewLicense_Command; 
     public ICommand AddNewLicense_Command 
     { 
      get { return addNewLicense_Command ?? (addNewLicense_Command = new RelayCommand<Customer>(n =>{ AddNewLicense_Execute(n); }));} 
     } 

     public void AddNewLicense_Execute(Customer customer) 
     { 
      Licenses.Add(new License("Paint")); 
     } 
    } 


    public class License : NotifyBase 
    { 
     private string product; 
     public string Product 
     { 
      get { return product; } 
      set { Set(ref product, value); } 
     } 

     public License(string product) 
     { 
      this.Product = product; 
     } 

    } 

} 

NotifyBase.cs

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Runtime.CompilerServices; 


namespace WpfApplication1.Helper 
{ 
    public abstract class NotifyBase : INotifyPropertyChanged 
    { 
     public event PropertyChangedEventHandler PropertyChanged; 

     protected void RaisePropertyChanged([CallerMemberName] string propertyName = null) 
     { 
      if (this.PropertyChanged != null) 
       this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 

     protected bool Set<T>(ref T field, T value, [CallerMemberName] string propertyName = null) 
     { 
      if (EqualityComparer<T>.Default.Equals(field, value)) return false; 
      field = value; 
      RaisePropertyChanged(propertyName); 
      return true; 
     } 
    } 
} 

下面是該解決方案的鏈接:Solution Files

回答

1

從你的代碼中,要更新LicensesCount當你設置新ObservableCollectionLicenses財產。當添加新的License對象時Licenses屬性不變。要正確更新LicensesCount,您應該聽取您的licenses可觀察收藏的CollectionChanged事件。

看起來應該呈三角這樣:

private ObservableCollection<License> licenses; 
public ObservableCollection<License> Licenses 
{ 
    get { return licenses ?? (licenses = CreateLicensesCollection()); } 
    set 
    { 
     Console.Write("Modified"); 
     Set(ref licenses, value); 
     RaisePropertyChanged(nameof(LicensesCount)); 
    } 
} 

private ObservableCollection<License> CreateLicensesCollection() 
{ 
    var collection = new ObservableCollection<License>(); 
    collection.CollectionChanged += (s, a) => RaisePropertyChanged(nameof(LicensesCount)); 
    return collection; 
} 
+0

我明白了,這清除了一切。感謝您的幫助。 有沒有辦法可以將它添加到ObservableObjectEx類?所以它適用於任何Observable對象的類。這樣做似乎更安全。 – JokerMartini

+0

對不起,我無法想象簡單的方法來自動化這... –

+0

諒解。儘管謝謝你的幫助!你的答案是正確的 – JokerMartini

0

你爲什麼不直接綁定到的ObservableCollection的Count財產?將xaml中的綁定源從LicensesCount更改爲Licenses.Count。由於ObservableCollection具有INotifyPropertyChanged的內置功能,因此不需要額外的編碼。