Serializing WPF DataTemplates and {Binding Expressions} (from PowerShell?) Serializing WPF DataTemplates and {Binding Expressions} (from PowerShell?) powershell powershell

Serializing WPF DataTemplates and {Binding Expressions} (from PowerShell?)


After all this time (and considering the number of views this question has), it's worth coming back here to note that this is fixed in PowerShell 3 -- I'm not sure if that's because they fixed a bug, or if it's because PS3 runs on .Net CLR 4 or what.

Regardless, if you add System.Xaml to the list of -reference assemblies, the code in the original question works as-is in PowerShell 3 and 4. I guess I'll mark this as the answer, mostly so people can stop coming here to try and answer it ;)


I'm no powershell dev, but have you tried escaping the { } with ` ? Perhaps it's trying to be clever and evaluate the binding as a powershell expression?


I am a little bit confused about the TypeConverter setup you're doing in XamlHelper's type initializer. What is BindingConverter supposed to do? Are you intending for the {Binding} markup extension to be handled the way it normally does in WPF?

In any case, markup extensions cannot round trip through XAML which is by design. The following excerpt from an MSDN page regarding XAML serialization limitations:

Common references to objects made by various markup extension formats, such as StaticResource or Binding, will be dereferenced by the serialization process. These were already dereferenced at the time that in-memory objects were created by the application runtime, and the Save logic does not revisit the original XAML to restore such references to the serialized output. This potentially freezes any databound or resource obtained value to be the value last used by the run-time representation, with only limited or indirect ability to distinguish such a value from any other value set locally. Images are also serialized as object references to images as they exist in the project, rather than as original source references, losing whatever filename or URI was originally referenced. Even resources declared within the same page are seen serialized into the point where they were referenced, rather than being preserved as a key of a resource collection

Given that, I'm not sure why on earth it should even work at all in a compiled application. But as I said, I must admit I am not sure what you're doing with the TypeConverter so maybe you've already addressed the above limitation.