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

《深入浅出WPF》读书笔记.11Template机制(上)

《深入浅出WPF》读书笔记.11Template机制(上)

背景

模板机制用于实现控件数据算法的内容与外观的解耦。

《深入浅出WPF》读书笔记.11Template机制(上)

模板机制

模板分类

数据外衣DataTemplate

常用场景

事件驱动和数据驱动的区别

示例代码

使用DataTemplate实现数据样式

<Window x:Class="TemplateDemo.DataTemplateView"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:TemplateDemo"mc:Ignorable="d"Title="DataTemplateView" Height="450" Width="800"><Window.Resources><local:AutoMaker2PhotoPathConverter x:Key="a2p"></local:AutoMaker2PhotoPathConverter><local:Name2PhotoPathConverter x:Key="n2p"></local:Name2PhotoPathConverter><DataTemplate x:Key="detailTemplate"><Border BorderBrush="Black"  BorderThickness="1" CornerRadius="6"><StackPanel Margin="5"><Image x:Name="img1" Height="250" Width="400" Source="{Binding Name,Converter={StaticResource n2p}}"></Image><StackPanel Orientation="Horizontal"><TextBlock Text="Name:" FontSize="25" FontWeight="Bold"></TextBlock><TextBlock x:Name="tblName" FontSize="25" FontWeight="Bold" Text="{Binding Name}"></TextBlock></StackPanel><StackPanel Orientation="Horizontal"><TextBlock Text="AutoMaker:" Margin="5"></TextBlock><TextBlock x:Name="tblAutoMaker" Margin="5" Text="{Binding AutoMaker}"></TextBlock><TextBlock Text="Year:" Margin="5"></TextBlock><TextBlock x:Name="tblYear" Margin="5" Text="{Binding Year}"></TextBlock><TextBlock Text="TopSpeed::" Margin="5"></TextBlock><TextBlock x:Name="tblTopSpeed" Margin="5" Text="{Binding TopSpeed}"></TextBlock></StackPanel></StackPanel></Border></DataTemplate><DataTemplate x:Key="carListItem"><Border BorderBrush="Black" CornerRadius="6" BorderThickness="1"><StackPanel Margin="5" Orientation="Horizontal"><Image Source="{Binding Name,Converter={StaticResource n2p}}" Width="64" Height="64"></Image><StackPanel><TextBlock Text="{Binding Name}"></TextBlock><TextBlock Text="{Binding Year}"></TextBlock></StackPanel></StackPanel></Border></DataTemplate></Window.Resources><Grid><Grid.ColumnDefinitions><ColumnDefinition Width="200*"></ColumnDefinition><ColumnDefinition Width="100*"></ColumnDefinition></Grid.ColumnDefinitions><UserControl x:Name="ucCarDetail" Grid.Column="0" ContentTemplate="{StaticResource  detailTemplate}" Content="{Binding ElementName=listBoxCar,Path=SelectedItem}"></UserControl><ListBox x:Name="listBoxCar" Grid.Column="1" ItemTemplate="{StaticResource carListItem}"></ListBox></Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;namespace TemplateDemo
{/// <summary>/// DataTemplateView.xaml 的交互逻辑/// </summary>public partial class DataTemplateView : Window{public DataTemplateView(){InitializeComponent();GetCarData();}public void GetCarData(){List<Car> cars = new List<Car>(){new Car{Name="avatar1",Year="1998",Automaker="CN",TopSpeed="300"},new Car{Name="avatar2",Year="1999",Automaker="CN",TopSpeed="350"},new Car{Name="avatar3",Year="2000",Automaker="CN",TopSpeed="400"}};this.listBoxCar.ItemsSource = cars;}}
}
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;namespace TemplateDemo
{public class L2BConver : IValueConverter{public object Convert(object value, Type targetType, object parameter, CultureInfo culture){return (int)value > 6 ? true : false;}public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture){throw new NotImplementedException();}}
}

代码说明

控件外衣ContentTemplate

用途

                                                                                   

使用blend观看控件内部
<Window x:Class="TemplateDemo.ControlTemplate"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:TemplateDemo"mc:Ignorable="d"Title="ControlTemplate" Height="300" Width="400"><Window.Resources><Style x:Key="RoundCornerTextBox" BasedOn="{x:Null}" TargetType="TextBox"><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="TextBox"><Border CornerRadius="5" x:Name="bd" SnapsToDevicePixels="True"BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}"><ScrollViewer x:Name="PART_ContentHost" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"></ScrollViewer></Border></ControlTemplate></Setter.Value></Setter></Style></Window.Resources><Window.Background><LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1"><GradientStop Color="Orange" Offset="0"></GradientStop><GradientStop Color="White" Offset="1"></GradientStop></LinearGradientBrush></Window.Background><StackPanel><TextBox Width="120" Height="40" Style="{StaticResource RoundCornerTextBox}" BorderBrush="Black" Margin="5"></TextBox><TextBox Width="120" Height="40" Style="{StaticResource RoundCornerTextBox}" BorderBrush="Black" Margin="5"></TextBox><Button Width="120" Height="40"  Content="点击一下" Margin="5"></Button></StackPanel>
</Window>

<Window x:Class="TemplateDemo.PanelTemplate"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:TemplateDemo"mc:Ignorable="d"Title="PanelTemplate" Height="450" Width="800"><Grid><ListBox><ListBox.ItemsPanel><ItemsPanelTemplate><StackPanel Orientation="Vertical"></StackPanel>              </ItemsPanelTemplate></ListBox.ItemsPanel><TextBlock Text="郭靖"></TextBlock><TextBlock Text="黄蓉"></TextBlock><TextBlock Text="杨康"></TextBlock><TextBlock Text="穆念慈"></TextBlock></ListBox></Grid>
</Window>


Git地址

GitHub - wanghuayu-hub2021/WpfBookDemo: 深入浅出WPF的demo

相关文章:

《深入浅出WPF》读书笔记.11Template机制(上)

《深入浅出WPF》读书笔记.11Template机制(上) 背景 模板机制用于实现控件数据算法的内容与外观的解耦。 《深入浅出WPF》读书笔记.11Template机制(上) 模板机制 模板分类 数据外衣DataTemplate 常用场景 事件驱动和数据驱动的区别 示例代码 使用DataTemplate实现数据样式…...

C语言程序设计(算法的概念及其表示)

一、算法的概念 一个程序应包括两个方面的内容: 对数据的描述:数据结构 对操作的描述:算法 著名计算机科学家沃思提出一个公式: 数据结构 +算法 =程序 完整的程序设计应该是: 数据结构+算法+程序设计方法+语言工具 广义地说,为解决一个问题而采取的方法和步骤…...

【最新华为OD机试E卷-支持在线评测】猜数字(100分)多语言题解-(Python/C/JavaScript/Java/Cpp)

🍭 大家好这里是春秋招笔试突围 ,一枚热爱算法的程序员 ✨ 本系列打算持续跟新华为OD-E/D卷的三语言AC题解 💻 ACM金牌🏅️团队| 多次AK大厂笔试 | 编程一对一辅导 👏 感谢大家的订阅➕ 和 喜欢💗 🍿 最新华为OD机试D卷目录,全、新、准,题目覆盖率达 95% 以上,…...

上海亚商投顾:深成指、创业板指均涨超1%,华为产业链反复活跃

上海亚商投顾前言&#xff1a;无惧大盘涨跌&#xff0c;解密龙虎榜资金&#xff0c;跟踪一线游资和机构资金动向&#xff0c;识别短期热点和强势个股。 一.市场情绪 大小指数昨日走势分化&#xff0c;沪指全天震荡调整&#xff0c;2800点失而复得&#xff0c;深成指、创业板指…...

【H2O2|全栈】Markdown | Md 笔记到底如何使用?【前端 · HTML前置知识】

Markdown的一些杂谈 目录 Markdown的一些杂谈 前言 准备工作 认识.Md文件 为什么使用Md&#xff1f; 怎么使用Md&#xff1f; ​编辑 怎么看别人给我的Md文件&#xff1f; Md文件命令 切换模式 粗体、倾斜、下划线、删除线和荧光标记 分级标题 水平线 引用 无序…...

C++17: 用折叠表达式实现一个IsAllTrue函数

前言 让我们实现一个 IsAllTrue 函数&#xff0c;支持变长参数&#xff0c;可传入多个表达式&#xff0c;必须全部计算为true&#xff0c;该函数才返回true。 本文记录了逐步实现与优化该函数的思维链&#xff0c;用到了以下现代C新特性知识&#xff0c;适合对C进阶知识有一定…...

【IPV6从入门到起飞】2-2 获取你的IPV6(Teredo隧道)

【IPV6从入门到起飞】2-2 获取你的IPV6&#xff08;Teredo隧道&#xff09; 1 打工人的忧伤2 Teredo介绍2.1 背景2.2 工作原理 3 Linux 服务器获取IPV63.1 安装3.2 设置开机自启动和启动3.3 开放防火墙 UDP 35443.4 查看IPV6以及ping包测试3.5 修改Teredo服务器3.6 重启服务3.7…...

Linux 安全弹出外接磁盘

命令行操作 首先&#xff0c;需要卸载硬盘上的所有分区&#xff0c;可以使用umount来卸载分区 清空系统缓存&#xff0c;将所有的数据写入磁盘 sync 列出已挂载的文件系统 使用lsblk或者df命令来查找要卸载的分区 lsblk or df -h确保没有文件正在使用 使用lsof 命令来…...

面试准备-6

NIO底层是用Selector、Channel和ByteBuffer来实现的。主线程在循环使用select方法进行阻塞等待&#xff0c;当有acceptable&#xff08;可连接&#xff09;、readable&#xff08;可读&#xff09;或者writable&#xff08;可写&#xff09;事件发生的时候&#xff0c;循环就会…...

context canceled 到底谁在作祟?

一、背景 在工作中&#xff0c;因报警治理标准提高&#xff0c;在报警治理的过程中&#xff0c;有一类context cancel报警渐渐凸显出来。 目前context cancel日志报警大致可以分为两类。 context deadline exceeded 耗时长有明确报错原因 context canceled 耗时短无明确报错…...

windows C++ 虚拟内存的按需调拨

虚拟内存的按需调拨 windows C 虚拟内存的按需调拨 文章目录 虚拟内存的按需调拨虚拟内存的按需调拨 虚拟内存的按需调拨 /*------------------------------------------------------------------------24-SEHAndMemory.cpp演示虚拟内存的按需调拨--------------------------…...

[杂项]pugi::xml获取xml中的注释节点

前言 想到学习xml时的一句话&#xff0c;xml中注释也会被算作一个节点。那么我们就可以通过 pugixml 把注释节点获取出来&#xff0c; <?xml version"1.0"?> <mesh name"mesh_root"><!--这是一个注释节点-->some text<![CDATA[so…...

Spring Boot Admin集成与自定义监控告警

目录 一.Spring Boot Admin集成 1.引入依赖 2.添加配置 3.监控界面 二.Spring Boot Admin告警机制 1. 基本告警机制 2. 配置告警 2.1 triggers触发器讲解 3. 自定义通知 3.1 Instance 对象 三.Spring Boot Admin支持的监控属性 1.常见的Spring Boot Admin监控属性 …...

如何恢复回收站中已删除/清空的文件

回收站清空后如何恢复已删除的文件&#xff1f;是否可以恢复永久删除的文件&#xff1f;或者最糟糕的是&#xff0c;如果文件直接被删除怎么办&#xff1f;本文将向您展示清空回收站后恢复已删除数据的最佳方法。 回收站清空后如何恢复已删除的文件&#xff1f; “回收站清空后…...

玩短视频素材都是在哪里找的?推荐几个热门的短视频素材下载渠道

亲爱的短视频创作爱好者们&#xff0c;你是否在寻找视频素材时感到苦恼&#xff0c;觉得选择有限&#xff1f;别担心&#xff0c;今天我要为大家介绍几个超级实用的视频素材下载平台&#xff0c;帮助你的视频创作事半功倍&#xff01; 蛙学网 我们首先要重点推荐的是蛙学网&am…...

ThinkPHP5 5.0.23-rce远程代码执行漏洞复现

启动环境&#xff0c;先关闭其他环境 启动 判断是否存在漏洞&#xff1a;访问/index.php?scaptcha页面&#xff0c;会出现报错 使用HackBar 插件发送 POST 请求 _method__construct&filter[]system&methodget&server[REQUEST_METHOD]dir 通过echo命令写入 Webshe…...

windows下安装并使用nvm

目录 一.准备工作&#xff1a;卸载node 卸载步骤 二.下载nvm 三.安装nvm 三.配置下载源【重要】 四.使用nvm安装node.js 五.nvm常用命令 六.卸载nvm 一.准备工作&#xff1a;卸载node 如果电脑上已经有node&#xff0c;那么我们需要先完全卸载node&#xff0c;再安装…...

mac m2 安装 nvm

踩坑-填坑 过程 红字都是 启动台-ohter-终端 里面直接输入就行了 /bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)" brew -v 重启终端 brew uninstall nvm brew install nvm 成功提示 > Summary &#x1f37a; /o…...

通信工程学习:什么是AN接入网络

AN接入网络 AN接入网络&#xff0c;全称Access Network&#xff0c;是电信部门业务节点与用户终端设备之间的实施系统。它可以部分或全部代替传统的用户本地线路网&#xff0c;并可包括复用、交叉连接和传输功能。以下是关于AN接入网络的详细解释&#xff1a; 一、AN接入网络的…...

MSCKF7讲:特征管理与优化

MSCKF7讲&#xff1a;特征管理与优化 文章目录 MSCKF7讲&#xff1a;特征管理与优化1 Feature.h2 OptimizationConfig3 initializePosition三角化LM优化3.1 计算归一化坐标深度初值generateInitialGuess① 理论推导② 代码分析 3.2 计算归一化误差cost① 理论推导② 代码分析 3…...

MPNet:旋转机械轻量化故障诊断模型详解python代码复现

目录 一、问题背景与挑战 二、MPNet核心架构 2.1 多分支特征融合模块(MBFM) 2.2 残差注意力金字塔模块(RAPM) 2.2.1 空间金字塔注意力(SPA) 2.2.2 金字塔残差块(PRBlock) 2.3 分类器设计 三、关键技术突破 3.1 多尺度特征融合 3.2 轻量化设计策略 3.3 抗噪声…...

鸿蒙中用HarmonyOS SDK应用服务 HarmonyOS5开发一个医院查看报告小程序

一、开发环境准备 ​​工具安装​​&#xff1a; 下载安装DevEco Studio 4.0&#xff08;支持HarmonyOS 5&#xff09;配置HarmonyOS SDK 5.0确保Node.js版本≥14 ​​项目初始化​​&#xff1a; ohpm init harmony/hospital-report-app 二、核心功能模块实现 1. 报告列表…...

2025 后端自学UNIAPP【项目实战:旅游项目】6、我的收藏页面

代码框架视图 1、先添加一个获取收藏景点的列表请求 【在文件my_api.js文件中添加】 // 引入公共的请求封装 import http from ./my_http.js// 登录接口&#xff08;适配服务端返回 Token&#xff09; export const login async (code, avatar) > {const res await http…...

【android bluetooth 框架分析 04】【bt-framework 层详解 1】【BluetoothProperties介绍】

1. BluetoothProperties介绍 libsysprop/srcs/android/sysprop/BluetoothProperties.sysprop BluetoothProperties.sysprop 是 Android AOSP 中的一种 系统属性定义文件&#xff08;System Property Definition File&#xff09;&#xff0c;用于声明和管理 Bluetooth 模块相…...

JDK 17 新特性

#JDK 17 新特性 /**************** 文本块 *****************/ python/scala中早就支持&#xff0c;不稀奇 String json “”" { “name”: “Java”, “version”: 17 } “”"; /**************** Switch 语句 -> 表达式 *****************/ 挺好的&#xff…...

智能分布式爬虫的数据处理流水线优化:基于深度强化学习的数据质量控制

在数字化浪潮席卷全球的今天&#xff0c;数据已成为企业和研究机构的核心资产。智能分布式爬虫作为高效的数据采集工具&#xff0c;在大规模数据获取中发挥着关键作用。然而&#xff0c;传统的数据处理流水线在面对复杂多变的网络环境和海量异构数据时&#xff0c;常出现数据质…...

Linux C语言网络编程详细入门教程:如何一步步实现TCP服务端与客户端通信

文章目录 Linux C语言网络编程详细入门教程&#xff1a;如何一步步实现TCP服务端与客户端通信前言一、网络通信基础概念二、服务端与客户端的完整流程图解三、每一步的详细讲解和代码示例1. 创建Socket&#xff08;服务端和客户端都要&#xff09;2. 绑定本地地址和端口&#x…...

宇树科技,改名了!

提到国内具身智能和机器人领域的代表企业&#xff0c;那宇树科技&#xff08;Unitree&#xff09;必须名列其榜。 最近&#xff0c;宇树科技的一项新变动消息在业界引发了不少关注和讨论&#xff0c;即&#xff1a; 宇树向其合作伙伴发布了一封公司名称变更函称&#xff0c;因…...

提升移动端网页调试效率:WebDebugX 与常见工具组合实践

在日常移动端开发中&#xff0c;网页调试始终是一个高频但又极具挑战的环节。尤其在面对 iOS 与 Android 的混合技术栈、各种设备差异化行为时&#xff0c;开发者迫切需要一套高效、可靠且跨平台的调试方案。过去&#xff0c;我们或多或少使用过 Chrome DevTools、Remote Debug…...

篇章二 论坛系统——系统设计

目录 2.系统设计 2.1 技术选型 2.2 设计数据库结构 2.2.1 数据库实体 1. 数据库设计 1.1 数据库名: forum db 1.2 表的设计 1.3 编写SQL 2.系统设计 2.1 技术选型 2.2 设计数据库结构 2.2.1 数据库实体 通过需求分析获得概念类并结合业务实现过程中的技术需要&#x…...