WPF ControlTemplate

ControlTemplate:控件模板,顾名思义也就是定制特定的控件供公共调用,有点类似WinForm中对一些通用控件进行重写使用。

ControlTemplate:控件模板主要有两个重要属性:VisualTree内容属性和Triggers触发器。定义控件模板也是对控件的视觉树和触发器进行重新定义,属性可以依赖原有控件的属性,只是根据需要调整的部分属性,

在Xaml文件的Resources中定义需要使用的模板,在需要使用模板的控件中将Template赋值为相应的控件模板x:key值实现控件的重定义。

以我们最常使用的Button为例,一般也都会都Button的控件模板作为资源进行全局通用资源,在具体使用的地方进行替换原有控件的Template:

<Window x:Class="WpfApp.MainWindow"
        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:WpfApp"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <Style TargetType="Button">
            <Setter Property="Width" Value="80"/>
            <Setter Property="Height" Value="30"/>
        </Style>
        <ControlTemplate TargetType="Button" x:Key="BtnTemplate">
            <Grid>
                <Ellipse Name="FaceEllispe" Width="{TemplateBinding Button.Width}" Height="{TemplateBinding Button.Height}" Fill="{TemplateBinding Button.Background}"/>
                <TextBlock Name="txtBlock" Margin="{TemplateBinding Button.Padding}" VerticalAlignment="Center" HorizontalAlignment="Center" Text="{TemplateBinding Button.Content}"/>
            </Grid>
            <ControlTemplate.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Button.Foreground" Value="Red"/>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </Window.Resources>
    <Grid>
        <StackPanel>
        <TextBlock Style="{StaticResource TitleText}" Name="textblock1">My Test WPF</TextBlock>
            <TextBlock>Click on my new Button</TextBlock>
            <Button Content="Test btn" Template="{StaticResource BtnTemplate}"/>
        </StackPanel>
    </Grid>
</Window>

对于页面中特定的某个Button进行结构重定义的话,也可以直接在Button控件下面改写Template:

<Window x:Class="WpfApp.MainWindow"
        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:WpfApp"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
 <Style TargetType="Button">
            <Setter Property="Width" Value="80"/>
            <Setter Property="Height" Value="30"/>
        </Style>
    </Window.Resources>
    <Grid>
        <StackPanel>
            <TextBlock Style="{StaticResource TitleText}" Name="textblock1">My Test WPF</TextBlock>
            <TextBlock>Click on my new Button</TextBlock>
            <Button Content="Test btn">
                <Button.Template>
                    <ControlTemplate>
                        <Grid>
                            <Ellipse Name="FaceEllispe" Width="{TemplateBinding Button.Width}" Height="{TemplateBinding Button.Height}" Fill="{TemplateBinding Button.Background}"/>
                            <TextBlock Name="txtBlock" Margin="{TemplateBinding Button.Padding}" VerticalAlignment="Center" HorizontalAlignment="Center" Text="{TemplateBinding Button.Content}"/>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter Property="Button.Foreground" Value="Red"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Button.Template>
            </Button>
        </StackPanel>
    </Grid>
</Window>

两种实现方式,采用哪种依赖于模板的通用性,当然即使都是单次使用也可以写成资源文件进行调用,简化Xaml复杂度。

时间: 2024-08-11 09:06:01

WPF ControlTemplate的相关文章

WPF ControlTemplate简介

一.简介 WPF包含数据模板和控件模板,其中控件模板又包括ControlTemplate和ItemsPanelTemplate,这里讨论一下ControlTemplate.其实WPF的每一个控件都有一个默认的模板,该模板描述了控件的外观以及外观对外界刺激所做出的反应.我们可以自定义一个模板来替换掉控件的默认模板以便打造个性化的控件.与Style不同,Style只能改变控件的已有属性值(比如颜色字体)来定制控件,但控件模板可以改变控件的内部结构(VisualTree,视觉树)来完成更为复杂的定制,

WPF ControlTemplate 控件模板

http://www.cnblogs.com/zhouyinhui/archive/2007/03/28/690993.html 模板与样式 它们可以调整控件的属性,但是样式不能使用全新的由不同元素组成的可视化树替代控件原来的外观. <Window x:Class="ApplyTemplateToControl.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation&quo

WPF的ControlTemplate和DataTemplate简介

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

WPF Template模版之DataTemplate与ControlTemplate【一】

WPF Template模版之DataTemplate与ControlTemplate[一] 标签: Wpf模版 2015-04-19 11:52 510人阅读 评论(0) 收藏 举报  分类: -------1.5 .NET(25)  版权声明:本文为博主郎涯工作室原创文章,未经博主允许不得转载. 目录(?)[+] WPF系统不但支持传统的Winfrom编程的用户界面和用户体验设计,更支持使用专门的设计工具Blend进行专业设计,同时还推出了以模板为核心的新一代设计理念. 1. 模板的内涵 作

WPF中ControlTemplate和DataTemplate的区别

下面代码很好的解释了它们之间的区别: <Window x:Class="WPFTestMe.Window12" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window12" Height="300&q

WPF Template模版之DataTemplate与ControlTemplate的关系和应用【二】

1. DataTemplate和ControlTemplate的关系 学习过DataTemplate和ControlTemplate,你应该已经体会到,控件只是数据的行为和载体,是个抽象的概念,至于它本身长成什么样子(控件内部结构),它的数据会长成什么样子(数据显示结构)都是靠Template生成的.决定控件外观的是ControlTemplate,决定数据外观的是DataTemplate,它们正是Control类的Template和ContentTemplate两个属性值 凡是Template,

WPF后台写ControlTemplate总结

这段时间写ControlTemplate的时候发现绑定的时候有些问题需要总结: 实例ControlTemplate如下: <UserControl x:Class="ArcGISWpfMarkTest.TestSymbol" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006

从0 开始 WPF MVVM 企业级框架实现与说明 ---- 第四讲 WPF中 ControlTemplate

上讲我们介绍了DataTemplate,现在我们就介绍下ControlTemplate,可能后面大多在编码时候会出现一些英文,工作习惯,请见谅. ControlTemplate: 控件的外观,也就是控件是什么样子 后面在我们这个项目中会大量用到这个东西, 现在我大概介绍一下你怎样去使用一个控件模板,首先你得定义一个控件模板,基本格式如下: <ControlTemplate x:Key="按钮控件模板名称"> 在这里就可以去定义你当前这个模板的具体样式,比如你定义一个按钮的样

WPF:在ControlTemplate中使用TemplateBinding

A bit on TemplateBinding and how to use it inside a ControlTemplate. Introductio Today I'll try to write a bit on TemplateBinding and how to use it inside a ControlTemplate.TemplateBinding is a type of binding used mainly for template scenarios. Here