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>
相关推荐
FuckPatience2 小时前
WPF Telerik.Windows.Controls.Data.PropertyGrid 自定义属性编辑器
wpf
almighty278 小时前
C#WPF控制USB摄像头参数:曝光、白平衡等高级设置完全指南
开发语言·c#·wpf·usb相机·参数设置
军训猫猫头16 小时前
12.NModbus4在C#上的部署与使用 C#例子 WPF例子
开发语言·c#·wpf
我要打打代码16 小时前
在WPF项目中使用阿里图标库iconfont
wpf
拾忆,想起2 天前
Redisson 分布式锁的实现原理
java·开发语言·分布式·后端·性能优化·wpf
weixin_464078072 天前
wpf依赖注入驱动的 MVVM实现(含免费源代码demo)
wpf
beyond谚语2 天前
一、WPF入门介绍+Grid和StackPanel布局介绍+实战模拟Notepad++页面布局
wpf
CPU不够了2 天前
WPF常见问题清单
wpf·自适应
beyond谚语2 天前
二、WPF——Style样式玩法(通过资源字典将Style独立,全局调用)
wpf
光辉岁月~2 天前
使用CalcBinding实现复杂逻辑绑定
wpf