DataTemplate作用是布局+数据绑定

使用DataTemplate

同时完成样式布局和数据绑定

<Window.Resources>
<DataTemplate x:Key="PersonDataTemplate">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" Text="{Binding Name}"></TextBlock>
<TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Age}"></TextBlock>
<TextBlock Grid.Row="1" Grid.Column="0" Text="{Binding Hometown}"></TextBlock>
</Grid>
</DataTemplate>
</Window.Resources>
<Grid>
<ListBox ItemsSource="{Binding}" ItemTemplate="{StaticResource PersonDataTemplate}"></ListBox>
</Grid>

数据在后台赋值DataContext

效果

 

不使用DataTemplate

也可以不使用DataTemplate,使用表格控件中的"列"(DataGrid同)

<ListView HorizontalContentAlignment="Center" VerticalContentAlignment="Center" ItemsSource="{Binding }" HorizontalAlignment="Left" Name="listView1" VerticalAlignment="Top">
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn Header="姓名" DisplayMemberBinding="{Binding Name}"/>
<GridViewColumn Header="年龄" DisplayMemberBinding="{Binding Age}"/>
<GridViewColumn Header="年龄" DisplayMemberBinding="{Binding Hometown}"/>
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>

示例代码

https://github.com/zLulus/NotePractice/tree/dev3/WPF/WpfDemo/Template/DataTemplate

DataTemplate和ControlTemplate混用

针对ListBox等表格式控件
整个ListBox采用ControlTemplate,制订外观
里面的每一项采用DataTemplate,设置外观+数据绑定

DataTemplate的更多相关文章

  1. wpf ListView DataTemplate方式的鼠标悬停和选中更改背景色

    今天使用wpf技术弄一个ListView的时候,由于需求需要,需要ListView显示不同的数据模板,很自然的使用了DataTemplate方式来定义多个数据模板,并在ListView中使用ItemT ...

  2. [WPF系列]-数据邦定之DataTemplate 对 ItemsControl 进行样式和模板处理

    引言   即使 ItemsControl 不是 DataTemplate 所用于的唯一控件类型,将 ItemsControl 绑定到集合仍然很常见. 在 DataTemplate 中有哪些内容一节中, ...

  3. [WPF系列]-数据邦定之DataTemplate 对分层数据的支持

    到目前为止,我们仅讨论如何绑定和显示单个集合. 某些时候,您要绑定的集合包含其他集合. HierarchicalDataTemplate 类专用于 HeaderedItemsControl 类型以显示 ...

  4. [WPF系列]-数据邦定之DataTemplate 使用 DataTrigger 来应用属性值

    使用 DataTrigger 来应用属性值 当前表示不会告诉我们某个 Task 是家庭任务还是办公室任务.记住 Task 对象拥有类型为 TaskType 的 TaskType 属性,该类型是一个枚举 ...

  5. [WPF系列]-数据邦定之DataTemplate 根据对象属性切换模板

      引言 书接上回[WPF系列-数据邦定之DataTemplate],本篇介绍如何根据属性切换模板(DataTemplate)   切换模板的两种方式:   使用DataTemplateSelecto ...

  6. [WPF系列]-数据邦定之DataTemplate简介

    引言 WPF 数据模板化模型为定义数据的表示形式提供了很大的灵活性.WPF 控件有支持自定义数据表示形式的内置功能.首先介绍下如何定义Datatemplate,然后再介绍其他数据模板化功能,例如根据自 ...

  7. WPF中UserControl和DataTemplate

    最新更新: http://denghejun.github.io 前言 前言总是留给我说一些无关主题的言论,WPF作为全新Microsoft桌面或web应用程序显示技术框架, 从08年开始,一直到现在 ...

  8. toolkit:Accordion DataTemplate ListBox TextBlock Interaction.Triggers

    困扰好几个小时的问题终于解决了,本人系菜鸟,使用MVVM设计模式,绑定DataTemplate的Command,需要使用 DataContent的资源,否则无法触发ICommand ClickChil ...

  9. DataTemplate应用

    在WPF中,决定数据外观的是DataTemplate,即DataTemplate是数据内容的表现形式,一条数据显示成什么样子,是简单的文本还是直观的图形,就是由DataTemplate决定的.下面通过 ...

  10. WPF中ControlTemplate和DataTemplate的区别

    下面代码很好的解释了它们之间的区别: <Window x:Class="WPFTestMe.Window12" xmlns="http://schemas.micr ...

随机推荐

  1. $.getJSON 跨域

    //支持跨域 $.getJSON(url + '&callback=?', function(res) { if (res.status === 0) { console.log(res.re ...

  2. 【35.43%】【hdu 4347】The Closest M Points

    Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 98304/98304 K (Java/Others) Total Submissio ...

  3. centos7安装jdk+tomcat+nginx+mysql

    公司新项目要在linux下部署,搭建一下java运行环境,记录一下. 一.安装mysql 1去官网下载mysql,下载后并解压,我把mysql安装在/usr/local/mysql路径下 -linux ...

  4. PatentTips - Method and system for browsing things of internet of things on ip using web platform

    BACKGROUND The following disclosure relates to a method and system for enabling a user to browse phy ...

  5. [Node.js] Test Node RESTful API with Mocha and Chai

    In this lesson, we will use Chai's request method to test our Node application's API responses.By th ...

  6. 30行js rem弹性布局适配所有分辨率

    <script> /* # 按照宽高比例设定html字体, width=device-width initial-scale=1版 # @pargam win 窗口window对象 # @ ...

  7. MKNetworkKit的断点续传SIDownloader下载

    comefrom:http://cache.baiducontent.com/c?m=9f65cb4a8c8507ed4fece763105392230e54f73d6f8b9042238fce098 ...

  8. IT增值服务实践心得体会:企业客户的钱比个人客户好赚得多

    友情提示 本人喜欢直言不讳,不喜欢拐弯抹角.喜欢从客观和主观.自身和他人等多种角度去探讨问题.如有不当之处,欢迎吐槽. 若干心得体会1.企业客户的钱更好赚,个人客户的钱很难.  为什么这么说呢? a. ...

  9. Archive for the ‘Erlang’ Category 《Erlang编程指南》读后感

    http://timyang.net/category/erlang/ 在云时代,我们需要有更好的能利用多核功能及分布式能力的编程语言,Erlang在这方面具有天生的优势,因此我们始终对它保持强烈关注 ...

  10. NOIP模拟 poke - 贪心

    题目大意: 给一副牌(\(n \le 1000000, a_i \le 1000000\)),问最多能打出多少对对子(x, x)或者顺子(x, x + 1, x + 2). 题目分析: 感觉跟斗地主很 ...