<Window x:Class="MiscSamples.RuntimeXAML"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MiscSamples"
Title="RuntimeXAML" Height="300" Width="300">
<Window.Resources>
<local:stringToUIConverter x:Key="Converter"/>
</Window.Resources>
<UniformGrid Rows="1" Columns="2">
<ListBox ItemsSource="{Binding Projects}" x:Name="Lst"/>
<ContentPresenter Content="{Binding SelectedItem.DisplaySpecificationXml, ElementName=Lst, Converter={StaticResource Converter}}"/>
</UniformGrid>
</Window>
코드 뒤에 :
public partial class RuntimeXAML : Window
{
public List<Project> Projects { get; set; }
public RuntimeXAML()
{
InitializeComponent();
Projects = new List<Project>
{
new Project()
{
DisplaySpecificationXml =
"<StackPanel>" +
"<TextBlock FontWeight='Bold' Text='This is UserControl1'/>" +
"<ComboBox Text='ComboBox'/>" +
"</StackPanel>"
},
new Project()
{
}
};
DataContext = this;
}
}
변환기 :
public class stringToUIConverter: IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null || (!(value is string)))
return null;
var header = "<Grid xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' " +
"xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>";
var footer = "</Grid>";
var xaml = header + (string) value + footer;
var UI = System.Windows.Markup.XamlReader.Parse(xaml) as UIElement;
return UI;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
결과 :
나는 당신이 암시 적으로 사용자를 신뢰 희망
: 영리의 비트와 함께 , 하나는 공예가 될 수있다. 일부 절대적으로 끔찍한 결과와 xaml ... – JerKimball
이것은 외부 소비, 비즈니스 애플 리케이션을위한 애플 리케이션이 아닙니다. 사용자 정의 영역을 정의 할 사용자는 실제로 비즈니스를위한 개발자입니다. 여기에 엄청난 보안 위험은 없지만, 중요하다는 메모와 upvote의 가치에 감사드립니다. – Matt