当前位置: 首页 > 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…...

未来机器人的大脑:如何用神经网络模拟器实现更智能的决策?

编辑&#xff1a;陈萍萍的公主一点人工一点智能 未来机器人的大脑&#xff1a;如何用神经网络模拟器实现更智能的决策&#xff1f;RWM通过双自回归机制有效解决了复合误差、部分可观测性和随机动力学等关键挑战&#xff0c;在不依赖领域特定归纳偏见的条件下实现了卓越的预测准…...

RocketMQ延迟消息机制

两种延迟消息 RocketMQ中提供了两种延迟消息机制 指定固定的延迟级别 通过在Message中设定一个MessageDelayLevel参数&#xff0c;对应18个预设的延迟级别指定时间点的延迟级别 通过在Message中设定一个DeliverTimeMS指定一个Long类型表示的具体时间点。到了时间点后&#xf…...

IGP(Interior Gateway Protocol,内部网关协议)

IGP&#xff08;Interior Gateway Protocol&#xff0c;内部网关协议&#xff09; 是一种用于在一个自治系统&#xff08;AS&#xff09;内部传递路由信息的路由协议&#xff0c;主要用于在一个组织或机构的内部网络中决定数据包的最佳路径。与用于自治系统之间通信的 EGP&…...

Go 语言接口详解

Go 语言接口详解 核心概念 接口定义 在 Go 语言中&#xff0c;接口是一种抽象类型&#xff0c;它定义了一组方法的集合&#xff1a; // 定义接口 type Shape interface {Area() float64Perimeter() float64 } 接口实现 Go 接口的实现是隐式的&#xff1a; // 矩形结构体…...

c++ 面试题(1)-----深度优先搜索(DFS)实现

操作系统&#xff1a;ubuntu22.04 IDE:Visual Studio Code 编程语言&#xff1a;C11 题目描述 地上有一个 m 行 n 列的方格&#xff0c;从坐标 [0,0] 起始。一个机器人可以从某一格移动到上下左右四个格子&#xff0c;但不能进入行坐标和列坐标的数位之和大于 k 的格子。 例…...

vue3 字体颜色设置的多种方式

在Vue 3中设置字体颜色可以通过多种方式实现&#xff0c;这取决于你是想在组件内部直接设置&#xff0c;还是在CSS/SCSS/LESS等样式文件中定义。以下是几种常见的方法&#xff1a; 1. 内联样式 你可以直接在模板中使用style绑定来设置字体颜色。 <template><div :s…...

WEB3全栈开发——面试专业技能点P2智能合约开发(Solidity)

一、Solidity合约开发 下面是 Solidity 合约开发 的概念、代码示例及讲解&#xff0c;适合用作学习或写简历项目背景说明。 &#x1f9e0; 一、概念简介&#xff1a;Solidity 合约开发 Solidity 是一种专门为 以太坊&#xff08;Ethereum&#xff09;平台编写智能合约的高级编…...

Selenium常用函数介绍

目录 一&#xff0c;元素定位 1.1 cssSeector 1.2 xpath 二&#xff0c;操作测试对象 三&#xff0c;窗口 3.1 案例 3.2 窗口切换 3.3 窗口大小 3.4 屏幕截图 3.5 关闭窗口 四&#xff0c;弹窗 五&#xff0c;等待 六&#xff0c;导航 七&#xff0c;文件上传 …...

力扣热题100 k个一组反转链表题解

题目: 代码: func reverseKGroup(head *ListNode, k int) *ListNode {cur : headfor i : 0; i < k; i {if cur nil {return head}cur cur.Next}newHead : reverse(head, cur)head.Next reverseKGroup(cur, k)return newHead }func reverse(start, end *ListNode) *ListN…...

2025年渗透测试面试题总结-腾讯[实习]科恩实验室-安全工程师(题目+回答)

安全领域各种资源&#xff0c;学习文档&#xff0c;以及工具分享、前沿信息分享、POC、EXP分享。不定期分享各种好玩的项目及好用的工具&#xff0c;欢迎关注。 目录 腾讯[实习]科恩实验室-安全工程师 一、网络与协议 1. TCP三次握手 2. SYN扫描原理 3. HTTPS证书机制 二…...