【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>

效果:

相关推荐
m0_720245014 小时前
1543.统计好三元组(简单)
开发语言·算法
叠层归一研究院5 小时前
AGI 系统(八):符号范畴嵌入函子 — SymCat ↪ C_M107 严格化
c语言·开发语言·人工智能·算法·transformer·agi
明朝百晓生6 小时前
Deep RL learning[2026/8]
开发语言·javascript·人工智能
weixin-a153003083166 小时前
python-装饰器
开发语言·python
我的xiaodoujiao7 小时前
快速学习Python基础知识详细图文教程17--类型注解和断点调试
开发语言·python·学习·测试工具
rockey6279 小时前
基于AScript的Lua脚本语言发布啦
c#·.net·lua·script·动态脚本
leoZ2319 小时前
AI 辅助开发的五道坎
开发语言·人工智能·视觉检测·bert·php·超分辨率重建·openvino
程序员老陆9 小时前
Qt的QThread::usleep和FFmpeg的libavutil模块的av_usleep哪个精度高一些?
开发语言·qt·ffmpeg·音视频
2601_9638699510 小时前
【计算机毕业设计】基于Java的相框定制系统的设计与实现
java·开发语言·课程设计
名字还没想好☜10 小时前
Go 的 unsafe.Pointer 实战:零拷贝 []byte↔string 转换与三条铁律
开发语言·后端·golang·go·unsafe