首页 > 其他分享 >【WPF】自定义按钮样式(添加依赖属性、圆角)

【WPF】自定义按钮样式(添加依赖属性、圆角)

时间:2024-03-16 20:34:56浏览次数:12  
标签:圆角 自定义 CustomButton CornerRadius 组件 WPF public 属性

参考

https://www.bilibili.com/video/BV13D4y1u7XX/?p=21

代码示例

1、自定义CustomButton按钮继承Button

namespace WpfStudy.Buttons
{
    public class CustomButton : Button
    {
        public CornerRadius CornerRadius
        {
            get { return (CornerRadius)GetValue(CornerRadiusProperty); }
            set { SetValue(CornerRadiusProperty, value); }
        }

        public static readonly DependencyProperty CornerRadiusProperty =
            DependencyProperty.Register("CornerRadius", typeof(CornerRadius), typeof(CustomButton));

        public Brush BackgroundOfMouseHover
        {
            get { return (Brush)GetValue(BackgroundOfMouseHoverProperty); }
            set { SetValue(BackgroundOfMouseHoverProperty, value); }
        }
        public static readonly DependencyProperty BackgroundOfMouseHoverProperty =
            DependencyProperty.Register("BackgroundOfMouseHover", typeof(Brush), typeof(CustomButton));

    }
}

2、创建资源字典

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:btns="clr-namespace:WpfStudy.Buttons"
                    >
    <Style TargetType="{x:Type btns:CustomButton}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type btns:CustomButton}">
                    <Border x:Name="border" Background="{TemplateBinding Background}" 
                        CornerRadius="{TemplateBinding CornerRadius}"
                        VerticalAlignment="{TemplateBinding VerticalAlignment}"
                        HorizontalAlignment="{TemplateBinding HorizontalAlignment}"
                        Width="{TemplateBinding Width}"
                        Height="{TemplateBinding Height}"
                        >
                        <TextBlock Text="{TemplateBinding Content}"
                                HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                TextAlignment="Center"
                                x:Name="textBlock"
                                />
                    </Border>

                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter TargetName="border" Property="Background" Value="{Binding BackgroundOfMouseHover,RelativeSource={RelativeSource TemplatedParent}}"/>
                            <Setter TargetName="textBlock" Property="Foreground" Value="Black"/>
                        </Trigger>
                        <Trigger Property="IsPressed" Value="True">
                            <Setter TargetName="border" Property="Background" Value="Red"/>
                            <Setter TargetName="textBlock" Property="Foreground" Value="#fff"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

3、App.xaml中在全局引入资源字典

<Application x:Class="WpfStudy.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WpfStudy"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/Style/CustomButtonStyle.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

4、使用自定义按钮

<Window x:Class="WpfStudy.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfStudy"
        xmlns:btns="clr-namespace:WpfStudy.Buttons"
        mc:Ignorable="d"
        WindowStartupLocation="CenterScreen"
        Title="MainWindow" d:DesignHeight="119.5" d:DesignWidth="248.5">
    <Grid>
        <btns:CustomButton Content="打开"
                           Background="Green"
                           Foreground="#fff"
                           HorizontalAlignment="Center"
                           VerticalAlignment="Center"
                           VerticalContentAlignment="Center"
                           HorizontalContentAlignment="Center"
                           Width="100"
                           Height="60"
                           CornerRadius="20"
                           BackgroundOfMouseHover="LightBlue"
                           />
    </Grid>

</Window>

思考

复杂、需要注意的地方:

  1. 依赖属性的命名格式、参数格式,可以使用propdp快捷键。

  2. 理解资源字典中的标签

  • 哪个标签嵌套哪个标签,分别是在哪一层使用哪个标签。
  • 绑定Button已有依赖属性时使用TemplateBinding,TemplateBinding的含义和用法。
  • 绑定CustomButton自定义依赖属性时使用Binding,注意Binding后面的格式,例如:
<Setter TargetName="border" Property="Background" 
Value="{Binding BackgroundOfMouseHover,RelativeSource={RelativeSource TemplatedParent}}"/>
  • 当触发器的Setter中不明确指定哪个组件的属性时,先为组件增加一个x:Name,再用TargetName指定组件的名字。
  1. 对齐属性的含义
    注意区分:
  • HorizontalAlignment
  • VerticalAlignment
  • HorizontalContentAlignment
  • VerticalContentAlignment

感觉是这样的:

  • HorizontalAlignment、VerticalAlignment用于描述组件自身位于上层组件的对齐情况。
    想象有一个外壳(Border标签),描述该外壳相对上层的位置。
    对于ControlTemplate也就只有一个Content,里面正好放一个大的外壳,用于描述该CustomButton。
  • HorizontalContentAlignment、VerticalContentAlignment用于描述组件内部的对齐情况。
    外壳内部内容相对于外壳的位置。

标签:圆角,自定义,CustomButton,CornerRadius,组件,WPF,public,属性
From: https://www.cnblogs.com/redcode/p/18077542

相关文章

  • 【WPF】Grid的用法
    只使用行<Grid><Grid.RowDefinitions><RowDefinition/><RowDefinition/></Grid.RowDefinitions><TextBlockText="AAAAA"Grid.Row="0"/><Text......
  • 【WPF】MVVM极其简单的例子
    目的:通过例子了解、理解MVVM的基本结构。ModelnamespaceWpfStudy.Model{publicclassUserModel{publicstringName{get;set;}}}ViewModelnamespaceWpfStudy.ViewModel{publicclassMainVM:NotifyPropBase{publicM......
  • Revit插件界面(WPF)白屏问题
    Revit插件界面(WPF)白屏问题问题描述最近客户反馈了一个问题,说新给到的Revit插件界面一片空白。很奇怪,这个插件测试过是没问题的,让客户看看以前版本的怎么样,发现也是空白的。询问近期是否有安装什么东西,得知安装了另一个插件;然后尝试卸载了再查看,发现好了。原因分析后来拿到......
  • 【WPF】设置全局样式
    目的:创建一个资源字典,然后自动引入到各个Window或UserControl中,可以随意使用。方式:创建Style文件夹在Style文件夹内创建一个ButtonStyleStyle.xaml的资源字典示例如下:<ResourceDictionaryxmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"......
  • 使用MQTT.fx和自定义Client(Ubuntu上实现)测试MQTT服务器(EMQX )
    目录概述1配置EMQX做MQTT服务器1.1登录EMQX1.2配置EMQX1.2.1配置客户端认证1.2.2创建用户2测试MQTT服务器2.1配置MQTT.fx工具2.2连接MQTT服务器3使用MQTT.fx发布和订阅信息3.1在MQTT.fx上发布信息3.2在MQTT.fx上订阅信息4Ubuntu上实现MQTTClient4......
  • 什么是C#的扩展方法,要怎么自定义使用
    介绍:C#中的扩展方法是一种特殊的静态方法,允许你向现有的类添加新的方法,而无需修改类的原始定义。它们使得可以在不修改原始类的情况下,给类添加新的行为。这种功能在编写库或者框架时非常有用,因为它允许你向已有的类添加新的功能,同时不会破坏现有的代码。原理:扩展方法......
  • 打造真实感十足的速度表盘:WPF实现动态效果与刻度绘制
     概述:这个WPF项目通过XAML绘制汽车动态速度表盘,实现了0-300的速度刻度,包括数字、指针,并通过定时器模拟速度变化,展示了动态效果。详细实现包括界面设计、刻度绘制、指针角度计算等,通过C#代码与XAML文件结合完成。新建WPF项目:在VisualStudio中创建一个新的WPF项目。......
  • WPF中轻松操控GIF动画:WpfAnimatedGif库详解
    概述:在WPF中使用`WpfAnimatedGif`库展示GIF动画,首先确保安装了该库。通过XAML设置Image控件,指定GIF路径,然后在代码中使用库提供的方法实现动画控制。这简化了在WPF应用中处理GIF图的过程,提供了方便的接口来管理动画播放和暂停。当使用 WpfAnimatedGif 库在WPF中显示GIF图动......
  • Qt Charts 自定义样式
    QtCharts是Qt框架中用于创建图表和图形可视化的模块,它提供了一些内置的图表类型,如折线图、柱状图、饼图等。如果你想要自定义QtCharts中图表的样式,你可以使用一些方法来实现。以下是一些自定义QtCharts样式的方法:1、使用QChart的样式属性: QtCharts中的QChart类具有许多属性......
  • WPF线程模型
    1.渲染系统概述WPF采用保留模式渲染系统(RetainedModeRenderingSystem),该系统可分为UI线程和复合线程两个主要部分,两者协作完成WPF应用程序的渲染工作。1.1立即模式GUI和保持模式GUI图形API可分为保留模式API和即时模式API。Direct2D是一种即时模式API。WPF......