zoukankan      html  css  js  c++  java
  • WPF Knowledge Points ControlTemplate和DataTemplate的区别

    该文章主要是转载和摘录(比如院子里mrfangzheng的博文),已有的文章已经说的很清楚了,不用做重复的功。

    1. A ControlTemplate is used to define/change the way a class derived from Control looks, such as Buttons, ListBox, ScrollBar. However, some "Controls" you use are not derived from Control: Best example is the TextBlock class. The TextBlock cannot have a ControlTemplate.
    2. DataTemplate is closelly linked with the “Content” in WPF. Certain controls are made to display "Content". Content can be anything, such as Image, Text, Object.... in fact, it is defined as an "object" type. The DataTemplate simply defines how a particular content (or content type) is meant to be displayed.
    3. HierarchicalDataTemplate is an extension of DataTemplate with an additional ItemSource property. It is used to render hierarchical data in controls like treeview or menu.
    Example: Usually, a string is displayed using a simple TextBlock... However, if you want a custom text layout, decoration, alignement, ... you would need to define an Alternate DataTemplate.

    1. ControlTemplate用来指定 Control 的可在其多个实例之间共享的可视结构,行为,和Trigger等方面。和创建自定义控件不同,在许多情况下,您都不需要编写自己的控件,您只是希望更改控件的可视化或替换现有控件的 ControlTemplate。使用ControlTemplate很简单,只需要设定它,然后赋值到Template属性下面即可。
    2. DataTemplate用来描述数据对象的可视结构。通常使用DataTemplate 指定数据的直观表示。使用DataTemplate很简单,只需要设定它,然后赋值到CellTemplate, ContentTemplate, ItemTemplate属性下面即可.

    1. ControlTemplate用于描述控件本身的视觉样式和行为. 使用TemplateBinding来绑定控件自身的属性, 比如{TemplateBinding Background}。
    2. DataTemplate用于描述控件的Content(数据对象)的视觉样式。 使用Binding来绑定数据对象的属性, 比如{Binding PersonName}。即ControlTemplate决定了控件本身的样子。DataTemplate决定了Content的样子。

    一般来说, ControlTemplate内有一个ContentPresenter, 这个ContentPresenter的ContentTemplate就是DataTemplate类型:

    Control类型
        - Template属性 (ControlTemplate类型)
            - ContentPresenter
                - ContentTemplate (DataTemplate类型)

    ContentControl类型
        - Template属性 (ControlTemplate类型) 继承自Control
        - ContentTemplate (DataTemplate类型)

    ItemsControl类型
        - Template属性 (ControlTemplate类型) 继承自Control
        - ItemsPanel属性 (ItemsPanelTemplate类型) 指定布局容器
        - ItemTemplate属性 (DateTemplate类型) 每个Item的Template

  • 相关阅读:
    malloc
    排序算法学习笔记(三)-- 快速排序
    排序算法学习笔记(二)-- 归并排序
    排序算法学习笔记(一)-- 冒泡排序,选择排序,插入排序,希尔排序
    网络协议笔记
    域名和IP地址并用的理由
    WebSocket协议介绍
    Ajax概念
    共享密钥加密、公开密钥加密及HTTPS混合加密
    HTTP缺点
  • 原文地址:https://www.cnblogs.com/KeithWang/p/2413773.html
Copyright © 2011-2022 走看看