WPF Style样式设置

1.本window设置样式

在这里插入图片描述

<Window x:Class="WPF_Study.MainWindow"
        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:WPF_Study"
        mc:Ignorable="d"
        Title="WPF入门" Height="600" Width="800">
    <!--Window 资源相关-->
    <Window.Resources>
        <!--Style 针对按钮类型 默认样式-->
        <Style TargetType="Button">
            <Setter Property="Background" Value="WhiteSmoke"/>
            <Setter Property="FontSize" Value="20"/>
            <Setter Property="Height" Value="40"/>
            <Setter Property="Width" Value="200"/>
            <Setter Property="Margin" Value="0,10, 20, 20"/>
        </Style>

        <!--Style 继承默认样式 定义特殊属性-->
        <Style x:Key="LoginStyle" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
            <Setter Property="Background" Value="Green"/>
        </Style>

        <!--Style 继承默认样式 定义特殊属性-->
        <Style x:Key="QuitStyle" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
            <Setter Property="Background" Value="Red"/>
        </Style>

    </Window.Resources>
    <Grid>
        <StackPanel>
            <!-- 设置单个按钮属性 背景色:Background 字体大小:FontSize 宽度:Width 高度:Height -->
            <Button Content="登录" Background="Gray" FontSize="20" Width="200" Height="40"/>

            <!-- 使用样式 LoginStyle -->
            <Button Style="{ StaticResource LoginStyle}" Content="登录"/>

            <!-- 使用样式 QuitStyle -->
            <Button Style="{ StaticResource QuitStyle}">
                <Button.Content>退出</Button.Content>
                <Button.FontSize>20</Button.FontSize>
            </Button>

            <!-- 使用默认样式 -->
            <Button Content="取消"/>

        </StackPanel>
    </Grid>
</Window>

2.全局样式设置

1.创建样式文件(BaseButtonStyle.xaml)

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

    <!--Style 针对按钮类型 默认样式-->
    <Style TargetType="Button">
        <Setter Property="Background" Value="WhiteSmoke"/>
        <Setter Property="FontSize" Value="20"/>
        <Setter Property="Height" Value="40"/>
        <Setter Property="Width" Value="200"/>
        <Setter Property="Margin" Value="0,10, 20, 20"/>
    </Style>

    <!--Style 继承默认样式 定义特殊属性-->
    <Style x:Key="LoginStyle" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
        <Setter Property="Background" Value="Green"/>
    </Style>

    <!--Style 继承默认样式 定义特殊属性-->
    <Style x:Key="QuitStyle" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
        <Setter Property="Background" Value="Red"/>
    </Style>
</ResourceDictionary>

2.设置全局样式(修改App.xaml)

<Application x:Class="WPF_Study.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WPF_Study"
             StartupUri="MainWindow.xaml">
    <!--StartupUri="MainWindow.xaml" -->

    <!--设置全局样式  -->
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <!-- 样式地址  -->
                <ResourceDictionary Source="/BaseButtonStyle.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>     
    </Application.Resources>
</Application>


3.设置选择样式

<Window x:Class="WPF_Study.StyleWindow"
        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:WPF_Study"
        mc:Ignorable="d"
        Title="StyleWindow" Height="450" Width="800">

    <Grid>
        <StackPanel>
            <!-- 设置单个按钮属性 背景色:Background 字体大小:FontSize 宽度:Width 高度:Height -->
            <Button Content="登录" Background="Gray" FontSize="20" Width="200" Height="40"/>

            <!-- 使用样式 LoginStyle -->
            <Button Style="{ StaticResource LoginStyle}" Content="登录"/>

            <!-- 使用样式 QuitStyle -->
            <Button Style="{ StaticResource QuitStyle}">
                <Button.Content>退出</Button.Content>
                <Button.FontSize>20</Button.FontSize>
            </Button>

            <!-- 使用默认样式 -->
            <Button Content="取消"/>

        </StackPanel>
    </Grid>
</Window>

相关推荐

  1. QT 设置鼠标样式

    2024-02-23 09:26:01       28 阅读
  2. Apache POI 设置样式

    2024-02-23 09:26:01       19 阅读
  3. Qt——设置字体样式

    2024-02-23 09:26:01       10 阅读
  4. Easy Excel设置表格样式

    2024-02-23 09:26:01       33 阅读
  5. HTML如何设置字体样式

    2024-02-23 09:26:01       11 阅读
  6. css 设置div阴影样式

    2024-02-23 09:26:01       9 阅读
  7. input输入框设置样式

    2024-02-23 09:26:01       8 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-02-23 09:26:01       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-23 09:26:01       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-23 09:26:01       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-23 09:26:01       18 阅读

热门阅读

  1. 中级.NET开发工程师面试经历

    2024-02-23 09:26:01       28 阅读
  2. MySQL

    2024-02-23 09:26:01       23 阅读
  3. 【npm install报错,如何解决记录】讲解

    2024-02-23 09:26:01       32 阅读
  4. 汽车会撞死人,应不应该限制汽车?

    2024-02-23 09:26:01       29 阅读
  5. lua 拓展math库,增加四舍五入函数 math.round

    2024-02-23 09:26:01       28 阅读
  6. Docker的优势及实际应用

    2024-02-23 09:26:01       23 阅读