我可以在运行时将XML/XAML转换为WPF控件吗?

我可以在运行时将XML/XAML转换为WPF控件吗?,wpf,xaml,Wpf,Xaml,有没有一种方法可以获取XML/XAML块并在运行时将其作为WPF控件加载 相关: 对。 您要查看的是类,特别是XamlReader.Load,例如: string xaml = @"<DataTemplate> @"<TextBlock Text=""{{Binding Converter={{StaticResource templatesConverter}}, {0} }}""/> @"</DataTemplate>"; Memory

有没有一种方法可以获取XML/XAML块并在运行时将其作为WPF控件加载


相关:

对。 您要查看的是类,特别是XamlReader.Load,例如:

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);
string xaml=
@"
@"
@"";
MemoryStream stream=新的MemoryStream(Encoding.ASCII.GetBytes(xaml));
ParserContext上下文=新的ParserContext();
context.xmlnsdirectionary.Add(“,”http://schemas.microsoft.com/winfx/2006/xaml/presentation");
context.xmlnsdirectionary.Add(“x”http://schemas.microsoft.com/winfx/2006/xaml");
DataTemplate DataTemplate=(DataTemplate)XamlReader.Load(流,上下文);

听起来这正是我想要的。谢谢