Click here to Skip to main content
13,899,599 members
Click here to Skip to main content
Add your own
alternative version


21 bookmarked
Posted 20 Jun 2008
Licenced CPOL

XamlWriter and Bindings Serialization

, 29 Jun 2008
Rate this:
Please Sign up or sign in to vote.
About serialization of Bindings to XAML with the standard MS XamlWriter.


This article is to show how we can save your dynamically created bindings to XAML by using the standard MS XamlWriter class in a simple and easy manner.


I am one of the developers of a project called FreeSCADA. One of the main parts of this project is the Graphical Designer which has some functionality to save vector graphics in XAML format. In the process of developing, I was faced with a problem. Bindings created from the code (or loaded with a XAML file) were not serialized in XAML format. I was trying to find solutions over the web, but found out only the following article. Unfortunately, this solution isn't suitable for more complex XAML files for some reasons. I did not believe that MS had some special internal solution to this problem, because I saw that the TemplateBinding extension could do such serialization perfectly, but Expression Binding could not. So, I downloaded the MS reference source code for debugging and started studying it. As a result, I found a very simple solution, which I will describe below.

Proposed solution

When I debugged code from MS, I found out that XamlWriter interprets the DependencyProperty value as a Binding. It checks this Binding type for the availability of some registered type converter which can transform this value to the MarkupExtension delivered type. If it is available, then XamlWriter converts this Binding to MarkupExtension and does further serialization.

So, if you want to save your Bindings, you can try the following simple code snippet. Basically, we need a class which looks something like this:

class BindingConvertor : ExpressionConverter
    public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
            return true;
        else return false;
    public override object ConvertTo(ITypeDescriptorContext context, 
                                     System.Globalization.CultureInfo culture, 
                                     object value, Type destinationType)
        if (destinationType == typeof(MarkupExtension))
            BindingExpression bindingExpression = value as BindingExpression;
            if (bindingExpression == null)
                throw new Exception();
            return  bindingExpression.ParentBinding;
        return base.ConvertTo(context, culture, value, destinationType);

After that, register this class as the type converter for the BindingExpression type. This can be done with the following code:

static class EditorHelper
    public static void Register <T, TC>()
        Attribute[] attr = new Attribute[1];
        TypeConverterAttribute vConv = new TypeConverterAttribute(typeof(TC));
        attr[0] = vConv;
        TypeDescriptor.AddAttributes(typeof(T), attr);

And call this function somewhere:

EditorHelper.Register <BindingExpression,BindingConvertor>();

After this, you must switch XamlWriter to the expression saving mode:

StringBuilder outstr=new StringBuilder();
//this code need for right XML fomating 
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.OmitXmlDeclaration = true;
XamlDesignerSerializationManager dsm = 
  new XamlDesignerSerializationManager(XmlWriter.Create(outstr, settings));
//this string need for turning on expression saving mode 
dsm.XamlWriterMode = XamlWriterMode.Expression;

XamlWriter.Save(MainPanel, dsm);

That's all you need. A working example can be found in the posted source code zip archive.

P.S.: I'm sorry for some inaccuracies which where there in the first version of this article.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer
Ukraine Ukraine
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionValidation role? Pin
ivanaGoce21-Jun-15 13:34
memberivanaGoce21-Jun-15 13:34 
QuestionFreeSCADA Pin
Member 1101469326-Aug-14 1:19
memberMember 1101469326-Aug-14 1:19 
AnswerRe: FreeSCADA Pin
AlexDov27-Aug-14 0:21
memberAlexDov27-Aug-14 0:21 
GeneralRe: FreeSCADA Pin
Member 1101469327-Aug-14 0:28
memberMember 1101469327-Aug-14 0:28 
QuestionCombox.SelectedValue not serialized Pin
erickozokas23-Apr-14 9:42
membererickozokas23-Apr-14 9:42 
QuestionWorking with ElementName Pin
Henrique Clausing31-Oct-11 6:39
memberHenrique Clausing31-Oct-11 6:39 
GeneralRe: Working with ElementName Pin
abdelkarim_se4-Aug-12 7:03
memberabdelkarim_se4-Aug-12 7:03 
GeneralCommand Bindings Pin
MichaelTR214-Mar-11 1:00
memberMichaelTR214-Mar-11 1:00 
GeneralItemsControl [modified] Pin
Darek Danielewski30-Mar-10 5:05
memberDarek Danielewski30-Mar-10 5:05 
GeneralRe: ItemsControl Pin
AlexDov30-Mar-10 18:42
memberAlexDov30-Mar-10 18:42 
GeneralRe: ItemsControl Pin
CD_Fighter30-Sep-11 0:57
memberCD_Fighter30-Sep-11 0:57 
BugRe: ItemsControl Pin
Henrique Clausing16-May-17 3:53
memberHenrique Clausing16-May-17 3:53 
QuestionHow to skip serialization of individual child controls? Pin
Member 18164265-Oct-09 3:30
memberMember 18164265-Oct-09 3:30 
QuestionSupport for StaticResource ? Pin
xradux200119-Jun-09 0:40
memberxradux200119-Jun-09 0:40 
GeneralAny UnRegister Pin
Atul C8-Apr-09 7:12
memberAtul C8-Apr-09 7:12 
GeneralRe: Any UnRegister Pin
DanielNeuwirth3-Sep-10 3:33
memberDanielNeuwirth3-Sep-10 3:33 
QuestionAny solution for serializing StaticResource? Pin
AmzSales10-Nov-08 12:09
memberAmzSales10-Nov-08 12:09 
AnswerRe: Any solution for serializing StaticResource? Pin
AlexDov10-Nov-08 19:10
memberAlexDov10-Nov-08 19:10 
GeneralRe: Any solution for serializing StaticResource? Pin
AmzSales10-Nov-08 19:40
memberAmzSales10-Nov-08 19:40 
GeneralRe: Any solution for serializing StaticResource? Pin
AlexDov10-Nov-08 21:56
memberAlexDov10-Nov-08 21:56 
At this moment I have a similar problem in my project. So when I found solution, i'll let you know...
GeneralRe: Any solution for serializing StaticResource? Pin
AmzSales11-Nov-08 4:05
memberAmzSales11-Nov-08 4:05 
AnswerRe: Any solution for serializing StaticResource? Pin
AlexDov14-Nov-08 1:24
memberAlexDov14-Nov-08 1:24 
GeneralRe: Any solution for serializing StaticResource? Pin
c0desmurf11-Nov-09 11:45
memberc0desmurf11-Nov-09 11:45 
GeneralRe: Any solution for serializing StaticResource? Pin
Mahesha99911-Mar-12 11:53
memberMahesha99911-Mar-12 11:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web04 | 2.8.190306.1 | Last Updated 29 Jun 2008
Article Copyright 2008 by AlexDov
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid