1

this code converts any controls or object to xml


if I saved this
screenshot


the saved xml would be looks like:

<Grid Name="g" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<Image Source="pack://application:,,,/WpfApplication1;component/Images/Picture3.jpg" Stretch="Fill" Name="image1" Width="200" Height="150" Margin="96,36,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" />
<Button Name="button1" Width="75" Height="23" Margin="352,71,0,0" HorizontalAlignment="Left" VerticalAlignment="Top">Button</Button>
<TextBox Name="textBox1" Width="120" Height="23" Margin="62,198,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" xml:space="preserve" />
<Label Name="label1" Height="28" Margin="270,213,0,0" HorizontalAlignment="Left" VerticalAlignment="Top">Label</Label>
</Grid>


I used this code to save this:

 System.IO.StreamWriter s = new System.IO.StreamWriter(@"d:\grid.xml");
            s.WriteLine(System.Windows.Markup.XamlWriter.Save(g));
            s.Close();

g is the name of the grid which contains all controls

my question:
can I reverse this, read this xml file and represent it on a window

Amged
  • 670
  • 1
  • 7
  • 19
  • thank you all my I found my answer here http://stackoverflow.com/questions/910814/loading-xaml-at-runtime – Amged Mar 25 '12 at 15:42

2 Answers2

1

Just use the XAML Reader Class.

shriek
  • 5,157
  • 2
  • 36
  • 42
0
var somecontrol = XamlReader.Load(@"<ItemsPanelTemplate
                xmlns=""http://schemas.microsoft.com/client/2007"" 
                xmlns:toolkit=""http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit"">
                <toolkit:WrapPanel /></ItemsPanelTemplate>");
Inga
  • 482
  • 4
  • 9