Click here to Skip to main content
13,357,773 members (61,317 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


6 bookmarked
Posted 21 May 2010

XAML Serialization

, 24 May 2010
Rate this:
Please Sign up or sign in to vote.
Serialization of WPF Framework Elements
In my last windows project I was supposed to create a Layout Management tool where user can create web layouts through a windows application.

This tool is having following functionality.

1- Create multiple rows.
2- Create multiple columns.
3- Merge Cell.
4- Split Cells

Requirements were similar to the table control present in the MS Office, where user can resize rows.

I implemented this using WPF Grid control and the resizing part is done through GridSplitter control. While implementing this tool the main issue I faced was to persist the layout, so that it can be loaded exactly same every time (mainly height/width of rows and columns and the exact location of the GridSplitter).

For persisting I thought of standard serialization technique, but it’s not possible to serialize a WPF framework element using binary or Xml serialization technique.

So finally I decided to store the XAML of the grid to the database and use the XAML to load the layout back.
I have used following code to generate the XAML code from the grid.

private XmlDocument GetXAMLFromGrid()
            StringBuilder outstr = new StringBuilder();
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            settings.OmitXmlDeclaration = true;
            XamlDesignerSerializationManager dsm = new XamlDesignerSerializationManager(XmlWriter.Create(outstr, settings));
            dsm.XamlWriterMode = XamlWriterMode.Expression;
            XamlWriter.Save(ivGrid, dsm);
            XmlDocument doc = new XmlDocument();
            return doc;

For restoring the Grid from the XAML I used following code.

private Grid GetGidFromXAML(string xamlText)
            Grid grid = (Grid)XamlReader.Parse(xamlText);
            return grid;


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


About the Author

Prakash Kalakoti
India India
Its me Smile | :)

You may also be interested in...

Comments and Discussions

QuestionImage bound to resource Pin
Maximus Byamukama3-Apr-12 0:38
memberMaximus Byamukama3-Apr-12 0:38 

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 | Terms of Use | Mobile
Web03 | 2.8.180111.1 | Last Updated 24 May 2010
Article Copyright 2010 by Prakash Kalakoti
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid