WPF 模板

一、DataTemplate(数据模板)
1、引用命名空间
xmlns:别名="clr-namespace:命名空间"

2、调用命名空间下的类别和属性
<Window.Resources>
<!--数据列表模板-->
<DataTemplate x:Key="模板名" DataType="{x:Type 别名/命名空间:类别}">
<Label Content="{Binding 属性}"></Label>
</DataTemplate>
</Window.Resources>

3、调用数据模板
<ListBox x:Name="ListBox" ItemTemplate="{StaticResource 模板名}" />

4、CS后台实例
using System;
namespace 命名空间
{
public class 类别
{
public string 属性 { get { return "我是属性"; } }
}
}

<!--在样式内创建数据列表模板-->
<Style TargetType="ListBox" x:Key="ListBox">
<Setter Property="ItemTemplate">
<Setter.Value>
<DataTemplate>
<Image Source="{Binding 属性}" Width="100" Height="100"/>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>

二、ControlTemplate(控件模板)
1、控件内定义模板
<Button Grid.Row="0" Grid.Column="1" Content="控件内定义模板">
<Button.Template>
<ControlTemplate TargetType="Button">
<!--定义视觉树-->
<Label Content="{TemplateBinding Content}"></Label>
</ControlTemplate>
</Button.Template>
</Button>

2、在资源创建控件模板
<Window.Resources>
<ControlTemplate TargetType="Button" x:Key="ButtonTemplate">
<!--定义视觉树-->
<Label Content="{TemplateBinding Content}"></Label>
</ControlTemplate>
</Window.Resources>

调用:

<Button Content="控件调用模板" Template="{StaticResource ButtonTemplate}" />

3、在样式内创建控件模板
<Style x:Key="TextBox" BasedOn="{x:Null}" TargetType="{x:Type TextBox}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Border x:Name="Bd"></Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

触发器
样式内定义触发器
<Style TargetType="Button" x:Key="TriggerButtonStyle" BasedOn="{StaticResource ButtonStyle}">
<Style.Triggers>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Foreground" Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>
在模板内定义触发器
<ControlTemplate TargetType="Button" x:Key="ButtonTemplate">
<!--定义视觉树-->
<Grid>
<Ellipse Name="faceEllipse" Width="{TemplateBinding Button.Width}" Height="{TemplateBinding Control.Height}" Fill="{TemplateBinding Button.Background}"/>
<TextBlock Name="txtBlock" Margin="{TemplateBinding Button.Padding}" VerticalAlignment="Center" HorizontalAlignment="Center" Text="{TemplateBinding Button.Content}" />
</Grid>
<!--定义视觉树_end-->
<!--定义触发器-->
<ControlTemplate.Triggers>
<Trigger Property="Button.IsMouseOver" Value="True">
<Setter Property="Button.Foreground" Value="Red" />
</Trigger>
</ControlTemplate.Triggers>
<!--定义触发器_End-->
</ControlTemplate>

mscorlib应用
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:local="clr-namespace:项目命名空间"

<sys:String x:Key="OK">
确定
</sys:String>

<sys:ArrayList x:Key="ds">
<local:Unit Year="2011年" Price="100"/>
<local:Unit Year="2010年" Price="120"/>
<local:Unit Year="2012年" Price="140"/>
<local:Unit Year="2013年" Price="160"/>
<local:Unit Year="2014年" Price="180"/>
</sys:ArrayList>
其中Unit的代码为:
public class Unit
{
public int Price { get; set; }
public string Year { get; set; }
}

时间: 2024-12-16 04:00:46

WPF 模板的相关文章

WPF快速入门系列(7)——深入解析WPF模板

一.引言 模板从字面意思理解是“具有一定规格的样板".在现实生活中,砖块都是方方正正的,那是因为制作砖块的模板是方方正正的,如果我们使模板为圆形的话,则制作出来的砖块就是圆形的,此时我们并不能说圆形的”砖块“不是砖块吧.因为形状只是它们的外观,其制作材料还是一样的.所以,模板可以理解为表现形式.WPF中的模板同样是表现形式的意思. 在WPF中包括三种模板:控件模板.数据模版和面板模板.它们都继承于FrameworkTemplate基类,其继承层次结果如下图所示: 从上图可以发现,Framewor

WPF模板(二)应用

原文:WPF模板(二)应用 本次内容来源于电子书,和上一篇一样. 在WPF中有三大模板ControlTemplate,ItemsPanelTemplate,DataTemplate.其中ControlTemplate和ItemsPanelTemplate是控件模板,DataTemplate是数据模板,他们都派生自FrameworkTemplate抽象类. 1.ControlTemplate ControlTemplate:控件模板主要有两个重要属性:VisualTree内容属性和Triggers

WPF模板(一)详细介绍

原文:WPF模板(一)详细介绍 本次随笔来源于电子书,人家的讲解很好,我就不画蛇添足了. 图形用户界面应用程序较之控制台界面应用程序最大的好处就是界面友好.数据显示直观.CUI程序中数据只能以文本的形式线性显示,GUI程序则允许数据以文本.列表.图形等多种形式立体显示. 用户体验在GUI程序设计中起着举足轻重的作用-----用户界面设计成什么样看上去才足够的漂亮?控件如何安排才简单易用并且少犯错误?这些都是设计师需要考虑的问题.WPF系统不但支持传统的Winfrom编程的用户界面和用户体验设计,

WPF模板

1:ControlTemplate 我们知道wpf的控件都是继承自Control,在Control类中有一个Template属性,类型就是ControlTemplate.那么利用这个ControlTemplate就可以彻底的颠覆控件的默认外观. <Window x:Class="WpfApplication1.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" x

Introduction to WPF Templates(WPF模板简介)

Introduction(简介) Windows Presentation Foundation allows the developer to completely change the look and feel of the controls. This is accomplished by using Control Templates. It means you can render your Button as an Ellipse which when hovered will c

wpf 模板选择器DataTemplateSelector及动态绑定,DataTemplate.Triggers触发器的使用

通常,如果有多个 DataTemplate 可用于同一类型的对象,并且您希望根据每个数据对象的属性提供自己的逻辑来选择要应用的 DataTemplate,则应创建 DataTemplateSelector.请注意,如果具有不同类型的对象,则可以对 DataTemplate 设置 DataType 属性.如果您执行了此操作,则无需创建 DataTemplateSelector.此外,如果对象类型相同但属性不同,也可以考虑使用 DataTrigger 或数据转换器. 通俗讲,就是根据不同的数据选择不

wpf 模板绑定父对象

有两种方式可以实现在模板中元素绑定到父对象 1.<ContentPresenter Margin=”{TemplateBinding Padding}”/> 2.Color=”{Binding RelativeSource={RelativeSource TemplatedParent},Path=Background.Color}”/> 原文地址:https://www.cnblogs.com/tianmochou/p/11412365.html

关于WPF中关于样式和模板的区别

百度了下,改天整理. WPF中关于样式和模板的区别: 回答一: 1.WPF样式类似于Web应用程序中的CSS,在WPF中可以为控件定义统一的样式(Style).样式属于资源的一种,例如为Button定义统一的背景颜色和字体: <Window.Resources> <Style  TargetType="Button"> <Setter Property="Background" Value="Yellow" />

WPF的ControlTemplate和DataTemplate简介

首先理清几个概念,Template.ControlTemplate.ContentTemplate.DataTemplate.ContentControl 这几个东西名字都差不多,意思感觉也接近,初次接触真的难以理解,那么现在开始区分了: 1.子类: ContentControl是Control的子类,专门用于显示内容的,如常用的Label就是ContentControl的子类 2.属性: Template 是Control类的一个属性: ContentTemplate是ContentContr