Simple WPF: WPF 自定义按钮外形

最新内容优先发布于个人博客:小虎技术分享站,随后逐步搬运到博客园。

WPF的按钮提供了Template模板,可以通过修改Template模板中的内容对按钮的样式进行自定义,完整代码Github自取。

使用Style定义扁平化的按钮样式

定义一个ButtonStyleDictonary.xaml资源字典文件,在Control Template中定义一个带Border的按钮,然后定义Trigger作为改变样式的触发器

xml 复制代码
<Style x:Key="FlatButtonStyle" TargetType="Button">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Border x:Name="border" Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}"
                        SnapsToDevicePixels="True">
                    <TextBlock Text="{TemplateBinding Content}"
                                Foreground="{TemplateBinding Foreground}"
                                VerticalAlignment="Center" 
                                HorizontalAlignment="Center"/>
                </Border>

                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter TargetName="border" Property="Background" Value="#2f96b4"/>
                    </Trigger>
                    <Trigger Property="IsPressed" Value="True">
                        <Setter TargetName="border" Property="Background" Value="red"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

WPF中使用ResourceDictonary 资源字典

引入在资源字典文件中定义公共的Template,然后在xaml窗口、自定义控件或者整个App当中调用

xml 复制代码
<Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="ButtonStyleDictonary.xaml"></ResourceDictionary>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Window.Resources>

然后就可以在窗体的xaml中应用刚才定义的属性了

xml 复制代码
<Button Style="{StaticResource FlatButtonStyle}" Width="64" Height="28">
     Hello
</Button>

使用Style和Polygon自定义Button的外形

xml 复制代码
<Style x:Key="ArrowButtonStyle" TargetType="Button">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Polygon x:Name="border" Fill="{TemplateBinding Background}"
                            Points="0,0 2,0 1,1" Stroke="Black" StrokeThickness="2"
                            SnapsToDevicePixels="True"
                            Stretch="Uniform"/>

                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter TargetName="border" Property="Fill" Value="gray"/>
                    </Trigger>
                    <Trigger Property="IsPressed" Value="True">
                        <Setter TargetName="border" Property="Fill" Value="red"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

效果如下

参考资料

WPF自定义控件与样式-自定义按钮(Button)
如何:使用应用程序范围的资源字典

相关推荐
月巴月巴白勺合鸟月半9 小时前
如果 我退休了
c#
专注VB编程开发20年9 小时前
.NET组件读取压缩包中的内存数据电子表格XLSX文件和图片,不需要解压
linux·服务器·windows·c#·.net·excel·zip
FuckPatience11 小时前
WPF TabControl页面绑定ItemsSource
wpf
爱上纯净的蓝天19 小时前
迁移面试题
java·网络·c++·pdf·c#
单单单单点20 小时前
C# 相机内存复用(减少图像采集耗时)以及行数复用
图像处理·计算机视觉·c#·相机
余衫马20 小时前
Mysql 5.7 与 SqlSugar 5.X 整合开发实战
mysql·c#·orm·sqlsugar
cplmlm1 天前
WPF+MVVM入门学习
c#·wpf
阿登林1 天前
C#微服务架构:实现指南与问题解决方案
微服务·架构·c#
山猪打不过家猪1 天前
(一)C#基础(异步)
c#
专注VB编程开发20年1 天前
对excel xlsx文件格式当成压缩包ZIP添加新的目录和文件后,OpenXml、NPOI、EPPlus、Spire.Office组件还能读出来吗
数据库·c#·excel