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

wpf之ContentPresenter

前言

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

1、ContentPresenter放置于中

1.1 创建模板

ContentPresenter常用于ControlTemplate中,它表示占位符,比如下面的代码中建立了一个模板,该模板中含有一个Border ,这个边框控件设置为红色,变宽厚度为10,并在其中放置了一个ContentPresenter ,并指定该控件代表的内容也就是Buttton的Content内容水平居中,垂直居中。

<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}"><Border  BorderBrush="Red"  BorderThickness="10"   Background="{TemplateBinding Background }" ><ContentPresenter    HorizontalAlignment="Center" VerticalAlignment="Center" /></Border></ControlTemplate></Window.Resources><StackPanel><Button   Height="300" Content="按钮" Background="Blue"  Template="{StaticResource btn_temp}"/></StackPanel>
</Window>

1.2 使用模板

最终在Window窗体中放置一个StackPanel,StackPanel中放置一个Button,由于该Button使用了模板btn_temp,所以在中间显示了Button的Content属性。
在这里插入图片描述

2、ContentPresenter放置于Grid中

2.1 创建模板

ContentPresenter常用于ControlTemplate中,它表示占位符,比如下面的代码中建立了一个模板,该模板中含有一个Grid,这个Grid有两行,第一行高度为100,第二行占用剩余所有高度,其中第一行放置了一个椭圆控件Ellipse ,并指定椭圆的填充色与使用该模板的控件的背景色相同;第2行放置了一个ContentPresenter ,并指定该控件代表的内容也就是Buttton的Content内容水平居中,垂直居中。

<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  ><Grid.RowDefinitions ><RowDefinition Height=" 100"/><RowDefinition Height="*"/></Grid.RowDefinitions ><Ellipse Grid.Row="0" Name="ellipse" Fill="{TemplateBinding Background}"/><ContentPresenter Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center" /></Grid></ControlTemplate></Window.Resources><StackPanel><Button  Height="300" Content="圆形按钮" Background="Blue"  Template="{StaticResource btn_temp}"/></StackPanel>
</Window>

2.2 使用模板

最终在Window窗体中放置一个StackPanel,StackPanel中放置一个Button,由于该Button使用了模板btn_temp,所以分为上下两部分,上部分是一个椭圆,下部分显示了Button的Content属性。
在这里插入图片描述

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

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

http://www.lryc.cn/news/606274.html

相关文章:

  • PyTorch深度学习快速入门学习总结(三)
  • 【机器学习篇】01day.python机器学习篇Scikit-learn入门
  • 机器学习①【机器学习的定义以及核心思想、数据集:机器学习的“燃料”(组成和获取)】
  • 运行图生视频/文生视频(Wan2.X等)的显卡配置总结
  • 基于deepseek的文本解析 - 超长文本的md结构化
  • CNN卷积神经网络之LeNet和AlexNet经典网络模型(三)
  • 深入解析LLM层归一化:稳定训练的关键
  • 模型优化——在MacOS 上使用 Python 脚本批量大幅度精简 GLB 模型(通过 Blender 处理)
  • 基于PyTorch利用CNN实现MNIST的手写数字识别
  • 【源力觉醒 创作者计划】对比与实践:基于文心大模型 4.5 的 Ollama+CherryStudio 知识库搭建教程
  • 如何系统性了解程序
  • 【Java安全】CC1链
  • <RT1176系列13>LWIP Ping功能入门级应用和基础API解析
  • MySQL 8.0 OCP 1Z0-908 题目解析(41)
  • python制作的软件工具安装包
  • XL2422 无线收发芯片,可用于遥控玩具和智能家居等应用领域
  • 5G-A技术浪潮勾勒通信产业新局,微美全息加快以“5.5G+ AI”新势能深化场景应用
  • 贝锐蒲公英X4 Pro 5G新品路由器:异地组网+8网口+双频WiFi全都有
  • 5G毫米波射频前端设计:从GaN功放到混合信号集成方案
  • arm架构系统打包qt程序--麒麟操作系统为例
  • [GESP202506 五级] 奖品兑换
  • Python列表完全指南:从基础到实战(2025版)
  • 八股训练--Spring
  • C#反射的概念与实战
  • 网络编程-IP
  • TCP窗口缩放配置在云服务器高延迟网络中的参数调整测试
  • Android端RTMP低延迟播放器在工业与智能场景下的架构与落地
  • 抓大鹅小游戏微信抖音流量主小程序开源
  • TGD第九篇:三维应用——视频边缘检测
  • 【AI论文】MUR:面向大型语言模型的动量不确定性引导推理