Svn资讯 | Svn使用
Svn介绍 | Svn配置
Php | Java | C/C++
UML | Dotnet|Div/Css
Linux | Windows
Unix | 服务器
MySql | Oracle | Sybase
Mssql | Access | DB2
VPS技术 | Svn下载
配置管理 | 免费资源
IT 业界 | 业界人物 | 互联网
家电动态 | 通讯动态 | 编程手册
搜索: 您的位置主页>Dotnet>C#>教你如何理解WPF中的Template类

教你如何理解WPF中的Template类

Svn中文网 www.svn8.com 2010-01-17 07:46:34   来源:bbs.svn8.com   作者:佚名 点击:
文章摘要:教你如何理解WPF中的Template类 解释Template定义 Template用于定义控件结构(Visual Tree),和Style有点容易混淆,每个控件初始没有Style属性,而在WPF中所有的控件都有默认的Template。 列举Template结构 FrameworkTemplate ControlTemplate ControlTemplate 添加了两个属性: TargetType,Triggers 举例各种Template 1

教你如何理解WPF中的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">