WPF和外部CSS文件

den*_*123 2 .net css c# wpf xaml

是否可以将样式定义存储在一个文件(XXXXX.css)中,然后在XAML文件中使用此文件?

Sim*_*ens 11

它不是CSS,但您可以在单独的文件中创建资源字典,如下所示:

<ResourceDictionary 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Style x:Key="MyStyledButton" TargetType="Button">
        .
        Put the details of your style here, just like you would for a normal style.
        .
    </Style>

</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)

然后,您可以通过在类上设置资源将该资源字典导入到其他XAML文件中,例如,如果您有Window类,则可以执行以下操作:

<Window.Resources>
    <ResourceDictionary Source="MyStylesFile.xaml" />
</Window.Resources>
Run Code Online (Sandbox Code Playgroud)

现在该样式对窗口内的任何控件都有效,就像您直接在窗口资源中指定了样式一样.