Is it possible to convert XML / XAML to WPF controls at runtime?

Is there a way to take a piece of XML / XAML and load it as WPF controls at runtime?


Related :
Can I use XamlReader.Load or InitializeFromXaml from a WPF window to define a window?

+3
source share
2 answers

Yes. what you want to see is the XamlReader class, specefically, XamlReader.Load

+4
source

eg:

string xaml = 
@"<DataTemplate>
    @"<TextBlock Text=""{{Binding Converter={{StaticResource templatesConverter}}, {0} }}""/>
  @"</DataTemplate>";

MemoryStream stream = new MemoryStream(Encoding.ASCII.GetBytes(xaml));

ParserContext context = new ParserContext();

context.XmlnsDictionary.Add("", "http://schemas.microsoft.com/winfx/2006/xaml/presentation");

context.XmlnsDictionary.Add("x", "http://schemas.microsoft.com/winfx/2006/xaml");

DataTemplate datatemplate = (DataTemplate)XamlReader.Load(stream, context);
0
source

Source: https://habr.com/ru/post/1734188/


All Articles