Skip to content

Instantly share code, notes, and snippets.

@eserozvataf
Last active December 14, 2015 14:08
Embed
What would you like to do?
Dynamic Xaml
using Sys = System;
using SysIO = System.IO;
using SysWinMarkup = System.Windows.Markup;
using SysXml = System.Xml;
using SysXmlLinq = System.Xml.Linq;
public static class XamlUtils {
public static object GetXamlObject(string source) {
SysXml.XmlDocument _xmlDocument = new SysXml.XmlDocument();
_xmlDocument.LoadXml(source);
return Xaml.GetXamlObject(_xmlDocument);
}
public static object GetXamlObject(SysXml.XmlDocument xmlDocument) {
const string xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation";
const string xmlns_x = "http://schemas.microsoft.com/winfx/2006/xaml";
if(string.IsNullOrEmpty(xmlDocument.DocumentElement.GetAttribute("xmlns"))) {
xmlDocument.DocumentElement.SetAttribute("xmlns", xmlns);
}
if(string.IsNullOrEmpty(xmlDocument.DocumentElement.GetAttribute("xmlns:x"))) {
xmlDocument.DocumentElement.SetAttribute("xmlns:x", xmlns_x);
}
SysIO.TextReader _stringReader = new SysIO.StringReader(xmlDocument.OuterXml);
SysXml.XmlReader _xmlReader = new SysXml.XmlTextReader(_stringReader);
return SysWinMarkup.XamlReader.Load(_xmlReader);
}
}
System.Windows.Controls.Grid _gridObject = XamlUtils.GetXamlObject("<grid><textblock>ok</textblock></grid>") as System.Windows.Controls.Grid;
this.Content = _gridObject;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment