Click here to Skip to main content
15,905,967 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Haii friends..

What my Requirement is Dynamically Create a XAML file with WPF and Load That File From the Directory where which is Saved.. From Database am Planning to add Controls to the XAML While Creating File. Is it Possible For Me to Save it in the Compiled Format and Load it at run time. please help me with sample code or Reference..

With regards anish.
Andy411 26-Apr-12 2:33am    
I'm not sure if I understand you. You want to add controls, e.g. Buttons or TextBoxes, to your form during runtime? This can be done in code behind without XAML.

1 solution

// Create the Button.
Button originalButton = new Button();
originalButton.Height = 50;
originalButton.Width = 100;
originalButton.Background = Brushes.AliceBlue;
originalButton.Content = "Click Me";

// Save the Button to a string .

//string savedButton = XamlWriter.Save(originalButton);
string savedButton = " ";
savedButton+="Click Me";

// Load the button
StringReader stringReader = new StringReader(savedButton);
XmlReader xmlReader = XmlReader.Create(stringReader);
Button readerLoadButton = (Button)XamlReader.Load(xmlReader);

Hope you will get some Idea from this example
Share this answer
anish.karunakaran 26-Apr-12 1:10am    
Sir.. You are Write. But I Wanna Create controls to a XAML file and Load the page with its CS as it is.. Is There any option to do So.. Please reply.

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

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900