Serializing data binding with binding in WPF (PropertyChangedEventManager)

I tried to show the list in Listbox by data binding. Here is my code.

[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) { //... } } 

It works very well in principle, but when I use data binding

 this.lbData.ItemsSource = myRecents; 

and try to serialize

 this.myRecents.Serialize(recentsPath); 

he does not cope with this error:

Enter "System.ComponentModel.PropertyChangedEventManager" in the assembly "WpfApplication1, Version = 3.0.0.0, Culture = neutral, PublicKeyToken = 31bf3856ad364e35" is not marked as serializable.

How can I handle this?

ps. I do not want to serialize the PropertyChangedEvent handler. I want to flag the [NonSerializable] attribute, but I don't know how to do this.

+8
serialization wpf
source share
1 answer

I want to flag the [NonSerializable] attribute, but I do not know how to do this.

In this case, you just need to mark the event with the [field:NonSerialized] attribute:

 [field:NonSerialized] public event PropertyChangedEventHandler PropertyChanged; 
+12
source share

All Articles