创建资源字典


然后在里面编写样式

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    
    <Style TargetType="{x:Type Button}">
        <Setter Property="Background" Value="WhiteSmoke"></Setter>
        <Setter Property="FontSize" Value="20"></Setter>
        <Setter Property="Margin" Value="20,10"></Setter>
    </Style>
    <Style x:Key="LoginStyle" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
        <Setter Property="Background" Value="LightBlue" ></Setter>
        <Setter Property="Width" Value="100"></Setter>
        <Setter Property="Height" Value="50"></Setter>
        <Setter Property="Content" Value="Login"></Setter>
    </Style>
    <Style x:Key="SigninStyle" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
        <Setter Property="Background" Value="LightBlue"></Setter>
        <Setter Property="Width" Value="100"></Setter>
        <Setter Property="Height" Value="50"></Setter>
        <Setter Property="Content" Value="SignIn"></Setter>
    </Style>
</ResourceDictionary>

配置App.xaml

<Application x:Class="WpfApp1.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WpfApp1"
             StartupUri="Window6.xaml">
    <Application.Resources>
         <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/BaseButton.xaml"></ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
         </ResourceDictionary>
    </Application.Resources>
</Application>

添加Application.Resources,在下面写东西就行了

使用样式

<Window x:Class="WpfApp1.Window8"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="Window8" Height="450" Width="800">
    <Grid>
        <StackPanel>
            <Button Content="登录" Style="{StaticResource LoginStyle}"></Button>
            <Button Content="注册" Style="{StaticResource SigninStyle}"></Button>
        </StackPanel>
    </Grid>
</Window>