当前位置: 首页 > news >正文

wpf之ControlTemplate

前言

我们在开发wpf程序的过程中,为了提高代码的重复利用率,经常会使用模板技术,本文就是介绍ControlTemplate以及样式Style的用法

1、ControlTemplate应用于当前控件

下面的代码在当前控件中建立了一个模板TargetType用于指定模板应用的控件类型,这里是Button,Button中放置了一个Border ,Border的背景色为红色,并且使用ContentPresenter 指定Button的Content内容位于Border控件的水平方向左边和垂直方向下边,运行效果如下图。

<Window x:Class="wpf之Style.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之Style"mc:Ignorable="d"Title="MainWindow" Height="450" Width="800"><StackPanel ><Button Height="50" Content="123"><Button.Template><ControlTemplate TargetType="Button"><Border Background="red"   ><ContentPresenter HorizontalAlignment="Left"  VerticalAlignment="Bottom" /></Border></ControlTemplate></Button.Template ></Button></StackPanel >
</Window>

在这里插入图片描述

2、根据Key来应用于指定控件

我们可以通过使用资源来给模板指定一个唯一的Key,比如我们可以建立多个不同样式的模板应用于Button控件,你想要哪个样式就通过这个key来使用对应的模板即可。下面的代码中通过ControlTemplate建立了一个模板,指定key为btn_temp,并且通过TargetType来指定模板应用的控件类型,并且这个模板使用一个Grid,Grid中放置一个椭圆,椭圆填充色通过TemplateBinding 来指定椭圆填充色和使用该模板的控件背景色相同,通过ContentPresenter 指定Button的content属性位于Grid的水平和垂直方向中间位置。

<Window x:Class="wpf之ControlTemplate.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之ControlTemplate"mc:Ignorable="d"Title="MainWindow" Height="450" Width="800"><Window.Resources><ControlTemplate x:Key="btn_temp" TargetType="{x:Type Button}"><Grid  ><Ellipse Name="ellipse" Fill="{TemplateBinding Background}"/><ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" /></Grid></ControlTemplate></Window.Resources><StackPanel><Button Height="50" Foreground="White"   Content="圆形按钮" Background="Blue"  Template="{StaticResource btn_temp}"/></StackPanel>
</Window>

最终通过StackPanel中放置一个Button,并且指定Button使用btn_temp模板。
在这里插入图片描述

3、结合Style根据Key来应用于指定控件

除了使用ControlTemplate 来彻底重写控件之外,我们还可以使用Style样式来指定模板控件原来默认的属性值,比如我们要建立一个Button控件的模板,我们可以通过Style来设置模板的背景色和字体颜色,从而应用于所有控件,如下。
下面的代码中使用样式指定背景色为红色,字体颜色为黄色;模板中放置一个Grid,背景色和Button的背景色相同,所以一开始颜色和Style指定的颜色相同为红色(这里必须指定背景色和控件相同,否则背景色就使用控件的默认背景色);ContentPresenter 指定Button的内容位于控件的水平和垂直中心;并且定义了一个触发器,鼠标放置在控件上方时,改变控件的背景色为绿色。

<Window x:Class="wpf之controlTemplate.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之controlTemplate"mc:Ignorable="d"Title="MainWindow" Height="450" Width="800"><Window.Resources><Style TargetType="Button" x:Key="btn_temp"><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="Button"><Grid   Background="{TemplateBinding Background }"    ><ContentPresenter HorizontalAlignment="Center"  VerticalAlignment="Center"  /></Grid><ControlTemplate.Triggers><Trigger Property="IsMouseOver" Value="True"><Setter Property="Background"   Value="Green"  /></Trigger></ControlTemplate.Triggers></ControlTemplate></Setter.Value></Setter><Setter Property="Foreground" Value="Yellow" ></Setter><Setter Property="Background"  Value="Red"  ></Setter></Style></Window.Resources><StackPanel ><Button Content="123" Width="200" Height="50" Style ="{StaticResource btn_temp}" /></StackPanel >
</Window>

软件启动后的效果
在这里插入图片描述
鼠标位于Button上方时。
在这里插入图片描述

马工撰写的年入30万+C#上位机项目实战必备教程(点击下方链接即可访问文章目录)

1、《C#串口通信从入门到精通》
2、《C#与PLC通信从入门到精通 》
3、《C# Modbus通信从入门到精通》
4、《C#Socket通信从入门到精通 》
5、《C# MES通信从入门到精通》
6、《winform控件从入门到精通》
7、《C#操作MySql数据库从入门到精通》

http://www.dtcms.com/a/307742.html

相关文章:

  • ACL 2024 大模型方向优秀论文:洞察NLP前沿​关键突破!
  • SpringMVC核心原理与实战指南
  • C++游戏开发(2)
  • 解决Android Studio中创建的模拟器第二次无法启动的问题
  • Android Studio怎么显示多排table,打开文件多行显示文件名
  • Android Studio 中Revert Commit、Undo Commit 和 Drop Commit 使用场景
  • 【智能体agent】入门之--1.初体验
  • HighgoDB查询慢SQL和阻塞SQL
  • 微信小程序性能优化与内存管理
  • HTTP 请求头(Request Headers)清单
  • 【13】大恒相机SDK C#开发 —— Fom1中实时处理的8个图像 实时显示在Form2界面的 pictureBox中
  • MySQL 中的聚簇索引和非聚簇索引的区别
  • 淘宝 API HTTP/2 多路复用与连接优化实践:提升商品数据采集吞吐量
  • Ceph、K8s、CSI、PVC、PV 深入详解
  • TTS语音合成|f5-tts语音合成服务器部署,实现http访问
  • 【n8n】如何跟着AI学习n8n【03】:HTTPRequest节点、Webhook节点、SMTP节点、mysql节点
  • 【11】大恒相机SDK C++开发 ——原图像数据IFrameData内存中上下颠倒,怎么裁剪ROI 实时显示在pictureBox中
  • 5G毫米波射频前端设计:从GaN功放到混合信号集成方案
  • 初始sklearn 数据集获取、分类、划分与特征工程
  • mysql笔记02:DML插入、更新、删除数据
  • 【读书笔记】Design Patterns (1994)✅
  • 贝锐蒲公英X4 Pro 5G新品路由器:异地组网+8网口+双频WiFi全都有
  • 大模型005
  • 反射之专题
  • C++:结构体(Structure)
  • Flux.1系列模型解析--Flux.1
  • OpenCV 中的「通道」(Channel)详解
  • C# 入门教程(四)委托详解
  • 国产芯+单北斗防爆终端:W5-D防爆智能手机,助力工业安全通信升级
  • Flutter Chen Generator - yaml配置使用