WPF:自定义按钮模板

1.WPF:自定义按钮模板

自定义封装的按钮属性可写在<Button.Template>中

Background="{TemplateBinding Background}"中的TemplateBinding代表使用按钮本身所使用的背景颜色 不在样式内修改背景颜色

例如:

csharp 复制代码
 <Button Width="300" Height="100" Content="自定义按钮" Background="Bisque" FontSize="23" Foreground="Orchid">
            <Button.Template>
                <ControlTemplate TargetType="{x:Type Button}">//代表指向的button按钮
                    <Border Background="{TemplateBinding Background}"  BorderBrush="Black" BorderThickness="4" CornerRadius="10">
                        <ContentPresenter HorizontalAlignment="Center" Content="14321412535" VerticalAlignment="Center"></ContentPresenter>    
                    </Border>
                    
                </ControlTemplate>
            </Button.Template>
        </Button>
2.通过事件处理改变属性

1.首先先定义按钮名称 <Border x:Name="button"

2.在 <ControlTemplate.Triggers>属性中写 <Trigger Property="IsMouseOver" value="True>写入通过什么触发事件

  1. <Setter TargetName="button" Property="Background" Value="black"/>根据TargetName找到对应的属性,并将其改变Property
csharp 复制代码
 <Button.Template>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Border x:Name="button" Background="{TemplateBinding Background}"  BorderBrush="Black" BorderThickness="4" CornerRadius="10">
          
                        <TextBlock Text="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="true">
                            <Setter TargetName="button" Property="Background" Value="black"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Button.Template>
相关推荐
Scout-leaf2 天前
WPF新手村教程(三)—— 路由事件
c#·wpf
柒.梧.4 天前
基于SpringBoot+JWT 实现Token登录认证与登录人信息查询
wpf
十月南城7 天前
Flink实时计算心智模型——流、窗口、水位线、状态与Checkpoint的协作
大数据·flink·wpf
听麟10 天前
HarmonyOS 6.0+ 跨端会议助手APP开发实战:多设备接续与智能纪要全流程落地
分布式·深度学习·华为·区块链·wpf·harmonyos
@hdd10 天前
Kubernetes 可观测性:Prometheus 监控、日志采集与告警
云原生·kubernetes·wpf·prometheus
zls36536510 天前
C# WPF canvas中绘制缺陷分布map
开发语言·c#·wpf
专注VB编程开发20年10 天前
c#Redis扣款锁的设计,多用户,多台电脑操作
wpf
闲人编程11 天前
定时任务与周期性调度
分布式·python·wpf·调度·cron·定时人物·周期性
zls36536511 天前
C# WPF canvas中绘制缺陷分布map并实现缩放
开发语言·c#·wpf
数据知道12 天前
PostgreSQL:Citus 分布式拓展,水平分片,支持海量数据与高并发
分布式·postgresql·wpf