在css中,可以把公用样式抽出放在一个单独的css文件里面。
在wpf中也可以做到类似的操作。
分离样式
把样式写在Style下的Button.xaml下:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="Styles.Button" TargetType="Button">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="#ff7300"></Setter>
<Setter Property="Foreground" Value="Red"></Setter>
</Trigger>
</Style.Triggers>
<Setter Property="Width" Value="100"></Setter>
<Setter Property="Height" Value="50"></Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border Background="{TemplateBinding Background}" BorderBrush="Black" BorderThickness="1">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"></ContentPresenter>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
引入样式
样式可以直接在App.xaml文件中引入,也可以在需要使用的页面的<Window.Resources> 下引入
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Style/Button.xaml"></ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
控件上使用
<Button Style="{StaticResource Styles.Button}">测试</Button>
标签:样式,xaml,引用,引入,wpf,css
From: https://www.cnblogs.com/boxrice/p/16823477.html