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)
如何:使用应用程序范围的资源字典

相关推荐
新手unity自用笔记10 小时前
项目-坦克大战学习-子弹的移动与销毁
笔记·学习·c#
qinzechen10 小时前
分享几个做题网站------学习网------工具网;
java·c语言·c++·python·c#
yufei-coder14 小时前
C# Windows 窗体开发基础
vscode·microsoft·c#·visual studio
dangoxiba14 小时前
[Unity Demo]从零开始制作空洞骑士Hollow Knight第十三集:制作小骑士的接触地刺复活机制以及完善地图的可交互对象
游戏·unity·visualstudio·c#·游戏引擎
AitTech14 小时前
深入理解C#中的TimeSpan结构体:创建、访问、计算与格式化
开发语言·数据库·c#
hiyo58518 小时前
C#中虚函数和抽象函数的概念
开发语言·c#
芝麻科技20 小时前
使用ValueConverters扩展实现枚举控制页面的显示
wpf·prism
开心工作室_kaic21 小时前
基于微信小程序的校园失物招领系统的设计与实现(论文+源码)_kaic
c语言·javascript·数据库·vue.js·c#·旅游·actionscript
时光追逐者1 天前
WaterCloud:一套基于.NET 8.0 + LayUI的快速开发框架,完全开源免费!
前端·microsoft·开源·c#·.net·layui·.netcore
friklogff1 天前
【C#生态园】打造现代化跨平台应用:深度解析.NET桌面应用工具
开发语言·c#·.net