教你如何理解WPF中的Template类

开发 后端
本文将你教你如何理解WPF中的Template类。文章由以下部分组成:解释Template定义、举Template结构、举例各种Template、我眼中的最佳实践。
解释Template定义

Template用于定义控件结构(Visual Tree),和Style有点容易混淆,每个控件初始没有Style属性,而在WPF中所有的控件都有默认的Template。

列举Template结构

image

FrameworkTemplate & ControlTemplate

ControlTemplate 添加了两个属性: TargetType,Triggers

 

举例各种Template 1. ControlTempalte - 自定义BUTTON

"WpfApplication1.Window2"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window2" Height="300" Width="300">
    
        "ItemsPanelTemplate1">
            "True" Orientation="Horizontal"/>
        
    
    
        "10,10,22,73" Name="listBox1" ItemsPanel="{DynamicResource ItemsPanelTemplate1}" />
    

效果:

image

2. ItemsTemplate - 自定义ListBox对齐方式 

"WpfApplication1.Window2"    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window2" Height="300" Width="300">
    
        "ItemsPanelTemplate1">
            "True" Orientation="Horizontal"/>
        
    
    
        "10,10,22,73" Name="listBox1" ItemsPanel="{DynamicResource ItemsPanelTemplate1}" />
    

TIP:     VirtualizingStackPanel 可以替换为其他ItemsPanel,如WrapPanel,StackPanel,UnifromGrid等。

3. DataTemplate - 自定义ListBox ItemTemplate

页面代码

"WpfApplication1.Window2"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window2" Height="351" Width="473" xmlns:WpfApplication1="clr-namespace:WpfApplication1" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d">
    
        "ItemsPanelTemplate1">
            "Auto" Height="Auto" Columns="5" Rows="3"/>
        
        "DataTemplate1">
            "111" Width="119" x:Name="grid" Background="#33ECF678">
                "Left" Margin="8,8,0,0" VerticalAlignment="Top" Width="103" Height="96" OpacityMask="#FFFFFFFF" Source="{Binding Path=ImageSource, Mode=Default}"/>
            
        
        "MyDataDS" ObjectType="{x:Type WpfApplication1:MyData}" d:IsDataSource="True"/>
    
    
        "listbox1" Margin="10,10,8,8" ItemsSource="{Binding}"  ItemsPanel="{DynamicResource ItemsPanelTemplate1}" ItemTemplate="{DynamicResource DataTemplate1}" Background="#3FDEF5E3" />
    

逻辑代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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;
using System.IO;

namespace WpfApplication1
{
    /// 
    /// Interaction logic for Window2.xaml
    /// 
    public partial class Window2 : Window
    {
        public Window2()
        {

            InitializeComponent();


            listbox1.DataContext = GetPictures();
            
            
        }

        private IEnumerable GetPictures()
        {
            foreach(string str in Directory.GetFiles(@"C:\Users\Public\Pictures\Sample Pictures"))
                yield return new MyData() {ImageSource = str};
        }


    }

    public class MyData
    {
        public string ImageSource { get; set; }
    }
}

效果

image

我眼中的最佳实践

1. Please USE Blend to Customize Templates

请使用Blend来自定义模板

2. USE  BasedOn Property Of Style

使用Style上的BasedOn属性

3. 使用共享资源,达到样式&Template重用

HOW: 在App.xaml中设置独立资源,或者使用外部资源

【编辑推荐】

  1. 详解Silverlight和WPF互相扩展
  2. Java代码的静态编译和动态编译中的问题比较
  3. 为WPF项目创建单元测试
责任编辑:彭凡 来源: cnblogs
相关推荐

2009-09-23 10:14:22

Hibernate

2024-03-15 09:44:17

WPFDispatcherUI线程

2020-08-23 11:32:21

JavaScript开发技术

2009-11-24 09:23:14

生成PHP类文件

2021-05-06 09:18:18

SQL自连接数据

2021-11-26 00:05:56

RabbitMQVirtualHostWeb

2022-08-31 12:57:58

PythonTemplate文件报告

2009-12-24 16:57:53

WPF密码

2024-04-17 09:27:22

WPF工具Template

2010-10-29 11:05:44

职场

2021-02-25 10:20:26

Java接口代码

2009-12-23 10:20:27

WPF类层次

2015-03-23 09:33:43

Java抽象类Java接口Java

2011-04-28 09:23:36

REST

2016-10-25 14:27:32

metaclasspython

2014-03-12 10:19:54

iOS对象

2011-04-14 15:55:35

WPF.NET

2022-04-27 08:55:01

Spring外部化配置

2020-10-21 08:05:45

Scrapy

2021-06-12 09:39:50

Python字典数据类型Python基础
点赞
收藏

51CTO技术栈公众号