c# wpf style 简单试验

1.概要

wpf style 用来控制控件的样式

2.代码

<Window x:Class="WpfApp2.Window5"
        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:WpfApp2"
        mc:Ignorable="d"
        Title="Window5" Height="450" Width="800">
    <Window.Resources>
        <ResourceDictionary>
            <!--设置控件样式-->
            <Style TargetType="Button" x:Key="btnStyle">
                <!--Setter设置控件静态属性-->
                <Setter Property="FontSize" Value="16"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type Button}"  >
                            <Border x:Name="back" BorderBrush="{TemplateBinding Control.BorderBrush}" Background="Red"  CornerRadius="5">
                                <!--ControlPresenter:内容占位符-->
                                <ContentPresenter Content="{TemplateBinding ContentControl.Content}" HorizontalAlignment="Center" VerticalAlignment="Center" />
                            </Border>
                            <ControlTemplate.Triggers>
                                <!--Triggers设置控件的行为风格-->
                                <Trigger Property="IsPressed" Value="True">
                                    <Setter TargetName="back" Property="Background" Value="Orange"/>
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </ResourceDictionary>
    </Window.Resources>
    <StackPanel>
        <!--设置按钮圆角-->
        <Button  Content="样式1" Width="100" Style="{StaticResource btnStyle}" />
        <Button  Content="样式2" Width="100" Style="{StaticResource btnStyle}"  />
    </StackPanel>
</Window>

3.运行结果

相关推荐

  1. 简单试验:用Excel进行爬虫

    2024-04-06 06:26:11       24 阅读

最近更新

  1. TCP协议是安全的吗?

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

    2024-04-06 06:26:11       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-06 06:26:11       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-06 06:26:11       18 阅读

热门阅读

  1. Django -- 自动化测试

    2024-04-06 06:26:11       12 阅读
  2. Linux 中 .bashrc、.bash-profile 和 .profile 之间的区别

    2024-04-06 06:26:11       10 阅读
  3. 解决安卓手机系统文件夹看不到的问题

    2024-04-06 06:26:11       15 阅读
  4. 稀碎从零算法笔记Day40-LeetCode:加油站

    2024-04-06 06:26:11       15 阅读
  5. 0基础如何进入IT行业?

    2024-04-06 06:26:11       14 阅读
  6. AI赋能写作:探索设计模式的魅力

    2024-04-06 06:26:11       14 阅读
  7. 位运算 -力扣90. 颠倒二进制位

    2024-04-06 06:26:11       11 阅读
  8. 什么是Ajax?怎么使用?

    2024-04-06 06:26:11       12 阅读
  9. 达梦体系结构:内存架构

    2024-04-06 06:26:11       14 阅读