A common concern with DependencyObjects is that they are not serializable: the DependencyObject class is not decorated with the [Serializable] attribute. This can be a problem when implementing IEditableObject, where the typical pattern is to serialize the object and deserialize it to cancel an edit. So how do you handle DependencyObject?
The solution is straightforward: use XamlWriter and XamlReader.
C#
MyDependencyObject obj = new MyDependencyObject();
obj.WebSite = "https://www.meziantou.net";
string serializedString = XamlWriter.Save(obj);
serializedString contains:
XAML
<MyDependencyObject
Website="https://www.meziantou.com"
xmlns="clr-namespace:Meziantou;assembly=Meziantou" />
Do you have a question or a suggestion about this post? Contact me!