记录 | WPF基础学习自定义按钮

ops/2025/2/11 11:34:38/

目录

  • 前言
  • 一、
    • 解析
      • Button.Template
      • Border和TemplateBinding
  • 二、代码提供
  • 更新时间


前言

参考文章:
参考视频:【WPF入门教程 Visual Studio 2022】WPF界面开发入门

自己的感想
这里涉及到Template模板和事件。主要干两件事:1、template中的重写原button;2、添加鼠标悬浮和鼠标点击事件。


一、

在这里插入图片描述

<Window x:Class="WPF_Study.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:WPF_Study"mc:Ignorable="d"Title="WPF入门.txt" Height="600" Width="800"><Grid><Button Width="300" Height="100" Content="自定义按钮" Background="Green" FontSize="50" Foreground="White"><!--自定义Button--><Button.Template><ControlTemplate TargetType="Button"><Border x:Name="border" Background="{TemplateBinding Background}" BorderBrush="Black" BorderThickness="4" CornerRadius="10" HorizontalAlignment="Center" VerticalAlignment="Center"><TextBlock x:Name="txtContent" Text="{TemplateBinding Content}"/></Border><!--自定义触发器--><ControlTemplate.Triggers><Trigger Property="IsMouseOver" Value="True"><Setter TargetName="border" Property="Background" Value="red"/></Trigger><Trigger Property="IsPressed" Value="true"><Setter TargetName="txtContent" Property="TextBlock.Text" Value="已经点击"/></Trigger></ControlTemplate.Triggers></ControlTemplate></Button.Template></Button></Grid></Window>

解析

Button.Template

Button.Template:通过 ControlTemplate 完全覆盖按钮的默认外观。

<Grid><Button Width="300" Height="100" Content="自定义按钮" Background="Green" FontSize="50" Foreground="White"><!--自定义Button--><Button.Template><ControlTemplate TargetType="Button"><!-- 按钮模板内容 --></ControlTemplate></Button.Template></Button>
</Grid>

Border和TemplateBinding

使用 Border 作为按钮容器,设置圆角、黑色边框。

内部通过 TextBlock 显示按钮内容(Content 属性)。

TemplateBinding:将按钮的 Background 和 Content 属性动态绑定到模板元素。

二、代码提供

点击下载


更新时间

  • 2025-02-06:创建。

http://www.ppmy.cn/ops/157512.html

相关文章

vue中computed和watch的区别,以及适用场景

一、computed&#xff1a;通过属性计算而得来的属性 computed内部的函数在调用时不加()。computed是依赖vm中data的属性变化而变化的&#xff0c;也就是说&#xff0c;当data中的属性发生改变的时候&#xff0c;当前函数才会执行&#xff0c;data中的属性没有改变的时候&#…

防御综合实验

1、实验拓扑图 2、实验步骤 需求一&#xff1a;完成IP配置 FW OA Web DNS PC1 Client2 需求二&#xff1a;DHCP分配IP 需求三&#xff1a;配置安全区域 需求四&#xff1a;防火墙地址组信息 DMZ区域 TrustA TrustB 需求五&#xff1a;管理员 配…

【大模型】Ubuntu下安装ollama,DeepSseek-R1:32b的本地部署和运行

1 ollama 的安装与设置 ollama官网链接&#xff1a;https://ollama.com/ 在左上角的【Models】中展示了ollama支持的模型在正中间的【Download】中课可以下载支持平台中的安装包。   其安装和模型路径配置操作流程如下&#xff1a; ollama的安装 这里选择命令安装curl -fsSL …

使用Spring boot的@Transactional进行事务管理

文章目录 一、前置条件二、基本用法1. 在方法上添加注解2. 在类上添加注解 三、核心配置参数1. 传播行为&#xff08;Propagation&#xff09;2. 隔离级别&#xff08;Isolation&#xff09;3. 超时时间&#xff08;Timeout&#xff09;4. 只读模式&#xff08;readOnly&#x…

面试真题 | 超图骏科 C++

构造函数的类型及其描述 在C++中,构造函数是用于初始化对象的特殊成员函数。根据用途和参数的不同,可以将构造函数分为以下几种类型: 默认构造函数(Default Constructor) 描述:没有参数的构造函数。如果类中没有定义任何构造函数,编译器会自动生成一个默认构造函数。但…

JVM 中的各种收集器总结

在 Java 虚拟机&#xff08;JVM&#xff09;的垃圾回收体系中&#xff0c;垃圾收集器扮演着至关重要的角色&#xff0c;它们负责自动回收不再使用的内存空间&#xff0c;以确保 JVM 的高效运行。不同的垃圾收集器具有不同的特点和适用场景&#xff0c;了解它们的工作原理和特性…

随手记:小程序手机号一键登录

获取手机号码的前提&#xff1a; 非个人小程序 认证的小程序 非海外的企业认证 大致流程图 获取对应code <u-button type"primary" shape"circle" open-type"getPhoneNumber" lang"zh_CN" getphonenumber"getPhoneNumber&…

【AIGC魔童】DeepSeek v3提示词Prompt书写技巧

【AIGC魔童】DeepSeek v3提示词Prompt书写技巧 &#xff08;1&#xff09;基础通用公式&#xff08;适用80%场景&#xff09;&#xff08;2&#xff09;问题解决公式&#xff08;决策支持&#xff09;&#xff08;3&#xff09;创意生成公式&#xff08;4&#xff09;学习提升公…