views:

153

answers:

2

How can I load XAML which is from the DataBase into the grid control in C#, How can I load that xaml into Grid Control ?

+1  A: 

Check out the XamlReader.Load() function.

slugster
But how to assign that XAML to Grid Control?
Asim Sajjad
Did you visit the link? Once you have executed XamlReader.Load() you have a control that you can add to your object tree (i.e. you can add it as a child control to another already existing control). Go to the link, there is a code example at the bottom.
slugster
StringReader stringReader = new StringReader(savedButton);XmlReader xmlReader = XmlReader.Create(stringReader);Button readerLoadButton = (Button)XamlReader.Load(xmlReader);this the code which is there, I have visited it and I have visited it Again. my question is that I have Grid objGrid=new Grid();how can I load that XAML in the Grid. I don't know that will be there in the XAML , it could be viewBox, button any control.
Asim Sajjad
Use the *Children* property. Like this: Grid g = new Grid(); g.Children.Add(new Button()); Whatever you add has to derive from *UIElement*, which most controls do.
slugster
it mean there is no direct way to assign the XamlReader.Load() to the grid control , like the button contro which has the property Content , to which the XamlReader.Load() can be assign.
Asim Sajjad
I'm failing to see your problem here. The Grid and Button are different types of control, so they do things slightly differently - and i've shown you how to load some arbitrary xaml as a control in to the Grid. You can do it in one statement if you wish: *g.Children.Add((Button) XamlReader.Load("my XAML"));*
slugster
A: 

you can use XamlReader.Load method to load xaml dynamically. You cannot use any code behind if you are loading dynamic xaml.

Just create a stream and pass that stream to the load method of the xamlreader.

Kishore Kumar