【C# WPF】自定义按钮模板以及设置触发器

WPF入门:

模板思想,自定义一个模板类来实现重定义按钮。

xml 复制代码
<Grid>
    <Button Width="300" Height="100" Content="自定义按钮" Background="Pink" FontSize="50" Foreground="White">
        <Button.Template>
            <ControlTemplate TargetType="{x:Type Button}">
                <Border x:Name="border" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="10">
                    <TextBlock x:Name="txtConent" Text="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter TargetName="border" Property="Background" Value="black"/>
                        <Setter TargetName="txtConent" Property="FontSize" Value="20"/>
                    </Trigger>
                    <Trigger Property="IsPressed" Value="True">
                        <Setter TargetName="border" Property="Background" Value="White"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Button.Template>
    </Button>
</Grid>

1.解析:

按钮的大小、内容、前背景、字体大小的实现:

xml 复制代码
<Button Width="300" Height="100" Content="自定义按钮" Background="Pink" FontSize="50" Foreground="White">

定义一个按钮模板,在其中填写内容:

xml 复制代码
<Button.Template>
    <ControlTemplate TargetType="{x:Type Button}">
    ....
    </ControlTemplate>
</Button.Template>

按钮的外观,border是边框的意思,TemplateBinding是绑定的意思,不会直接将值给写死,在实现的时候传参调用。在border内部加入一个文本框。

xml 复制代码
<Border x:Name="border" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="10">
    <TextBlock x:Name="txtConent" Text="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>

设置两个触发器,在指针移入的时候会变色+字体变化,点击的时候会变色。

xml 复制代码
<ControlTemplate.Triggers>
    <Trigger Property="IsMouseOver" Value="True">
        <Setter TargetName="border" Property="Background" Value="black"/>
        <Setter TargetName="txtConent" Property="FontSize" Value="20"/>
    </Trigger>
    <Trigger Property="IsPressed" Value="True">
        <Setter TargetName="border" Property="Background" Value="White"/>
    </Trigger>
</ControlTemplate.Triggers>

效果:

相关推荐
故事和你911 小时前
洛谷-数据结构1-1-线性表1
开发语言·数据结构·c++·算法·leetcode·动态规划·图论
石榴树下的七彩鱼2 小时前
图片修复 API 接入实战:网站如何自动去除图片水印(Python / PHP / C# 示例)
图像处理·后端·python·c#·php·api·图片去水印
techdashen2 小时前
Rust项目公开征测:Cargo 构建目录新布局方案
开发语言·后端·rust
星空椰2 小时前
JavaScript 进阶基础:函数、作用域与常用技巧总结
开发语言·前端·javascript
忒可君2 小时前
C# winform 自制分页功能
android·开发语言·c#
Rust研习社2 小时前
Rust 智能指针 Cell 与 RefCell 的内部可变性
开发语言·后端·rust
南無忘码至尊3 小时前
Unity学习90天 - 第 6天 - 学习协程 Coroutine并实现每隔 2 秒生成一波敌人
学习·unity·c#·游戏引擎
leaves falling3 小时前
C++模板进阶
开发语言·c++
坐吃山猪4 小时前
Python27_协程游戏理解
开发语言·python·游戏
gCode Teacher 格码致知4 小时前
Javascript提高:小数精度和随机数-由Deepseek产生
开发语言·javascript·ecmascript