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

news2024/9/21 0:41:38

《深入浅出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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2115886.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

2024Mysql And Redis基础与进阶操作系列(1)作者——LJS[含MySQL的下载、安装、配置详解步骤及报错对应解决方法]

目录 1.数据库与数据库管理系统 1.1 数据库的相关概念 1.2 数据库与数据库管理系统的关系 1.3 常见的数据库简介 Oracle 1. 核心功能 2. 架构和组件 3. 数据存储和管理 4. 高可用性和性能优化 5. 安全性 6. 版本和产品 7. 工具和接口 SQL Server 1. 核心功能 2. 架构和组…

唯徳知识产权产权系统存在任意文件读取漏洞

漏洞描述 深圳市唯德科创信息有限公司&#xff08;以下简称&#xff1a;唯德&#xff09;于2014年在深圳成立&#xff0c;是专业提供企业、代理机构知识产权管理软件供应商&#xff0c;唯德凭借领先的技术实力和深厚的专利行业积累&#xff0c;产品自上市推广以来&#xff0c;…

一文讲懂Spring Event事件通知机制

目录 一 什么是spring event 二 怎么实现spring event 一 什么是spring event 我不会按照官方的解释来说什么是spring event&#xff0c;我只是按照自己的理解来解释&#xff0c;可能原理上会和官方有偏差&#xff0c;但是它的作用和功能就是这个&#xff0c;我更加偏向于从他…

CTK框架(三): 插件的安装

目录 1.方式1&#xff1a;使用ctk框架工厂&#xff0c;适用于调用普通的插件 1.1.步骤 1.2.实现 2.方法2&#xff1a;使用ctk框架启动器&#xff0c;适用于需要eventadmin时 2.1.实现 3.注意事项 1.方式1&#xff1a;使用ctk框架工厂&#xff0c;适用于调用普通的插件 1…

Linux服务器应急响应(下)

目录 介绍步骤 介绍 Linux alias命令用于设置指令的别名。 用户可利用alias&#xff0c;自定指令的别名。若仅输入alias&#xff0c;则可列出目前所有的别名设置。alias的效力仅及于该次登入的操作。若要每次登入是即自动设好别名&#xff0c;可在.profile或.cshrc中设定指令…

ggplot2 缩小的、带箭头的坐标轴 | R语言

1. 效果图 左侧为DimPlot2()效果图。 右侧为DimPlot()效果图&#xff0c;原图。 2. 代码 # DimPlot with 缩小的坐标轴 # # param scObject # param reduction # param group.by # param label # param raster # param legend.position # param ... # # return # expo…

OCC开发_变高箱梁全桥建模

概述 上一篇文章《OCC开发_箱梁梁体建模》中详细介绍了箱梁梁体建模的过程。但是&#xff0c;对于实际桥梁&#xff0c;截面可能存在高度、腹板厚度、顶底板厚度变化&#xff0c;全桥的结构中心线存在平曲线和竖曲线。针对实际情况&#xff0c;通过一个截面拉伸来实现全桥建模显…

长短期记忆神经网络-LSTM回归预测-MATLAB代码实现

一、LSTM简介&#xff08;代码获取&#xff1a;底部公众号&#xff09; 长短期记忆神经网络&#xff08;Long Short-Term Memory, LSTM&#xff09;是一种循环神经网络&#xff08;Recurrent Neural Network, RNN&#xff09;的变体。相比于传统的RNN&#xff0c;LSTM能够更好…

nvidia-smi 随机掉卡,error,禁用GSP功能

问题 NVIDIA 驱动中默认开启加载GPU卡的GSP功能&#xff0c;会随机导致在执行nvidia-smi命令的时候读取GPU卡为ERR状态&#xff0c;或者导致smi命令卡死&#xff1b; 如下图&#xff0c;以A800为例&#xff0c;Centos系统&#xff1b; 涉及到的包含以下型号的GPU卡&#xff…

C#中chart绘制曲线

官网资料&#xff1a;Chart 类 (System.Windows.Forms.DataVisualization.Charting) | Microsoft Learn 类的 Chart 两个重要属性是 Series 和 ChartAreas 属性&#xff0c;这两个属性都是集合属性。 Series集合属性存储Series对象&#xff0c;这些对象用于存储要显示的数据以…

2024年全新deepfacelive简易版-傻瓜式使用教程-采用AI换脸软件deepfacelive

# 2024年全新deepfacelive简易版-傻瓜式使用教程-采用AI换脸软件deepfacelive # 下载安装deepfacelive 官网下载&#xff1a; https://github.com/iperov/DeepFaceLive/ 下载好后放在至少有 30G以上的盘&#xff0c; # 启动使用-设置中文 解压好后&#xff0c;双击.bat批处理…

GPT系列之:GPT-1,GPT-2,GPT-3详细解读

一、GPT1 论文&#xff1a;Improving Language Understanding by Generative Pre-Training 链接&#xff1a;https://cdn.openai.com/research-covers/languageunsupervised/language_understanding_paper.pdf 启发点&#xff1a;生成loss和微调loss同时作用&#xff0c;让下…

java常用集合方法

目录 一、Iterator接口二、Iterable接口三、Collection接口四、Collection与Iterable关系 一、Iterator接口 Iterator 是一个集合迭代器接口&#xff0c;它提供了以下方法&#xff1a; 判断迭代器中是否还拥有元素&#xff0c;有则返回true&#xff0c;否则返回false boolean …

实验八 输入/输出流

实验目的及要求 目的&#xff1a;通过实验掌握java提供的输入/输出包中类的使用&#xff0c;特别是一些常用的类的方法的使用&#xff0c;运用流的概念实现对象的序列化。 要求&#xff1a; &#xff08;1&#xff09;编写程序使用BufferedReader和BufferedWriter对文件进行…

Java高级编程—网络编程(完整详解,包括UDP通信方式、TCP通信方式、TCP三次握手、TCP四次挥手,附有代码+案列)

文章目录 二十九.网络编程29.1 概述29.2 InetAddress29.3 UDP通信29.3.1 UDP通信发送数据29.3.2 UDP通信接收数据29.3.3 Test 29.4 UDP的三种通信方式29.4.1 单播29.4.2 组播29.4.3 广播 29.5 TCP通信29.6 TCP通信三次握手29.7 TCP通信四次挥手29.8 Test29.8.1 多次发送 二十九…

Java数据结构(九)——选择排序、堆排序

文章目录 选择排序算法介绍代码实现复杂度和稳定性 堆排序算法介绍代码实现复杂度和稳定性 选择排序 算法介绍 选择排序是一种简单直观的排序算法。它的工作原理是&#xff1a;首先在未排序序列中找到最小&#xff08;大&#xff09;元素&#xff0c;存放到排序序列的起始位置…

【二分查找】锦集

二分查找锦集 二分前言1. 二分查找1.1 题目来源1.2 题目描述1.3 代码展示 2. 在排序数组中查找元素的第一个和最后一个位置2.1 题目来源2.2 题目描述2.3 解题分析 3. 搜索插入位置3.1 题目来源3.2 题目描述3.3 解题分析 4. x 的平方根4.1 题目来源4.2 题目描述4.3 解题分析 5. …

Oracle rman 没有0级时1级备份和0级大小一样,可以用来做恢复 resetlogs后也可以

文档说了 full backup 不能 用于后续的level 1&#xff0c;没说level 1没有level 0 是不是level 1就是level 0&#xff1f; 1级备份变0级的原因 及 Enabling Change Tracking生效没有-CSDN博客 这个文档说明1级备份时没有找到0级就是0级备份&#xff0c;可以用来完整恢复的。…

春日美食家:SpringBoot网上订餐系统

1 绪论 1.1 研究背景 随着互联网技术的快速发展&#xff0c;网络时代的到来&#xff0c;网络信息也将会改变当今社会。各行各业在日常企业经营管理等方面也在慢慢的向规范化和网络化趋势汇合[13]。电子商务必将成为未来商务的主流&#xff0c;因此对于餐饮行业来说&#xff0c;…

51单片机的无线病床呼叫系统【proteus仿真+程序+报告+原理图+演示视频】

1、主要功能 该系统由AT89C51/STC89C52单片机LCD1602显示模块温湿度传感器模块矩阵按键时钟模块等模块构成。适用于病床呼叫系统、16床位呼叫等相似项目。 可实现基本功能: 1、LCD1602实时显示北京时间、温湿度信息、呼叫床位等信息&#xff1b; 2、DHT11采集病房温湿度信息&…