从System.Windows.Style对象生成Xaml的工具

本文关键字:Xaml 工具 对象 System Windows Style | 更新日期: 2023-09-27 18:08:15

我有一个编程生成的System.Windows.Style类型对象,我想导出它作为XAML代码,有人知道有什么工具可以做到这一点吗?我想这有点像连载,但我不想自己写,如果已经有了的话。

谢谢!

从System.Windows.Style对象生成Xaml的工具

您可以使用XamlWriter:

var style = new Style(typeof(Control));
style.Setters.Add(new Setter(Control.BackgroundProperty, Brushes.Red));
var xaml = XamlWriter.Save(style);
上面的代码创建了这个XAML:
<Style TargetType="Control"
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
    <Style.Resources>
        <ResourceDictionary />
    </Style.Resources>
    <Setter Property="Panel.Background">
        <Setter.Value>
            <SolidColorBrush>#FFFF0000</SolidColorBrush>
        </Setter.Value>
    </Setter>
</Style>