C# WPF和外部CSS文件
是否可以将样式定义存储在一个文件(XXXXX.css)中,然后在XAML文件中使用此文件?它不是css,但您可以在单独的文件中创建一个资源字典,如下所示:C# WPF和外部CSS文件,c#,.net,css,wpf,xaml,C#,.net,Css,Wpf,Xaml,是否可以将样式定义存储在一个文件(XXXXX.css)中,然后在XAML文件中使用此文件?它不是css,但您可以在单独的文件中创建一个资源字典,如下所示: <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Style
<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>
.
把你的风格的细节放在这里,就像你会为一个正常的风格。
.
然后,您可以通过设置类上的资源将该资源字典导入到其他XAML文件中,例如,如果您有一个窗口类,您可以执行以下操作:
<Window.Resources>
<ResourceDictionary Source="MyStylesFile.xaml" />
</Window.Resources>
现在,该样式对窗口中的任何控件生效,就像您直接在窗口资源中指定了样式一样