2011-09-10 47 views
8

我試圖通過數據綁定向列表框顯示一個列表。這是我的代碼。序列化WPF中的數據綁定ObservableCollection(PropertyChangedEventManager)

[Serializable] 
public class RecordItem : INotifyPropertyChanged 
{ 
    //implements of INotifyPropertyChanged 
    public event PropertyChangedEventHandler PropertyChanged; 
    protected void Notify(string propName) { if (this.PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propName)); } } 
} 


[Serializable] 
public class Records : ObservableCollection<RecordItem> 
{ 
    public UOCRecords() { } 

    public void Serialize(string path) 
    { 
     BinaryFormatter binForm = new BinaryFormatter(); 
     using (FileStream sw = File.Create(path)) 
     { 
      binForm.Serialize(sw, this); 
      sw.Close(); 
     } 
    } 

    public static UOCRecords Deserialize(string path) 
    { 
     //... 
    } 
} 

它非常好,基本上,但是使用數據時,我結合

this.lbData.ItemsSource = myRecents; 

,並嘗試進行序列化

this.myRecents.Serialize(recentsPath); 

它失敗,此錯誤:

Type 'System.ComponentModel.PropertyChangedEventManager' in Assembly 'WpfApplication1, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' is not marked as serializable.

我該如何處理它?

ps。我不想序列化一個PropertyChangedEvent處理程序。我想標記[NonSerializable]屬性,但我不知道如何做到這一點。

回答

12

I want to marking [NonSerializable] attribute to that, but I don't know how to that.

在這種情況下,你只需要與[field:NonSerialized]屬性來標誌事件:

[field:NonSerialized] 
public event PropertyChangedEventHandler PropertyChanged; 
相關問題