DataTemplate的用法

WPF 模板主要分为两大类:
1.ControlTemplate: 控件的外观,也就是控件是什么样子。
2.DataTemplate: 是数据内容的表现,一条数据显示成什么样子。

(1)DataTemlate数据模板常用的地方有以下几处:
1.ContentControl的ContentTemplate属性
2.ItemsControl的ItemTemplate属性。
3.GridViewColumn的CellTemplate属性。
2. 实例代码
<Window x:Class="WpfApplication18.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<StackPanel x:Name="stackPanel">
<ListBox Margin="10" ItemsSource="{Binding}" >
<ListBox.ItemTemplate>
<DataTemplate >
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Name}" Margin="10"></TextBlock>
<TextBlock Text="{Binding Path=Score}" Margin="10"></TextBlock>
<CheckBox IsChecked="{Binding Path=Gender}" Margin="10"></CheckBox>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</Window>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication18
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var students = new List<Student>
{
new Student{Name="Jack",Score=80,Gender=true},
new Student{Name="Tom",Score=60,Gender=false},
new Student{Name="David",Score=80,Gender=true},
};
this.stackPanel.DataContext = students;
}
}

public class Student
{
public string Name { get; set; }

public int Score { get; set; }

public bool Gender { get; set; }
}
}

时间: 2024-08-11 01:35:34

DataTemplate的用法的相关文章

DataTemplate和ControlTemplate联系与区别

---恢复内容开始--- 正如标题中的两个拼接的单词所说,DataTemplate就是数据显示的模板,而ControlTemplate是控件自身的模板.(个人理解,错误请指出,谢谢) 我们看这二者在两类不同的控件中如何使用: 一:ItemsControl 我们可以利用ControlTemplate来设置控件外表,用DataTemplate来填充控件内容. <Page.Resources> <DataTemplate x:Key="dataTemplate"> &

关于WPF中Popup中的一些用法的总结

Popup控件是一个常用的非常有用的控件,顾明思义就是弹出式控件,首先我们来看看MSDN对它的解释吧,表示具有内容的弹出窗口,这个是非常重要的控件,我们看看它的继承关系吧: System.Object  System.Windows.Threading.DispatcherObject    System.Windows.DependencyObject      System.Windows.Media.Visual        System.Windows.UIElement       

WPF之DataTemplate的使用

一 概要 通过实例演示DataTemplate的基本用法,并通过实例来说明DataTemplate相关知识点. 二 实例操作 1 新建WPF应用程序WpfTemplateExp,程序结构如下图所示. 图1 程序结构图 程序最终的运行效果如下图所示. 图2 程序运行效果图 程序完成的功能是: 通过列表显示学生信息,并在选中某学生时,在列表下方的文本框中显示该生的总评信息. 2 新建实体类Student(类文件名为Student.cs),详细代码如下所示. //*******************

UWP入门(十二)--数据绑定用法

原文:UWP入门(十二)--数据绑定用法 主要几个元素: Template DataTemplate ItemSource 数据绑定是一个数据提取的方法,能使数据和UI上的控件紧密相连,下面的Demo是这样的: 有许多书的集合,书 类中有图片.标题.作者和ID,把它成现在GridView 控件上,每次点击GridView 的时候动态显示书本信息 github 代码 效果图: 原理图: 1. Template 为GridView 创建一个Template,决定每个独立的图书对象如何呈现在屏幕上 <

WPF的ListView控件自定义布局用法实例

本文实例讲述了WPF的ListView控件自定义布局用法.分享给大家供大家参考,具体如下: 概要: 以源码的形式贴出,免得忘记后,再到网上查资料.在VS2008+SP1环境下调试通过 引用的GrayscaleEffect模块,可根据参考资料<Grayscale Effect...>中的位置下载. 正文: 如何布局是在App.xaml中定义源码如下 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27

转:WPF中ListBox的创建和多种绑定用法

先从最容易的开始演示ListBox控件的创建. Adding ListBox Items下面的代码是向ListBox控件中添加多项ListBoxItem集合.XAML代码如下:<ListBox Margin="10,10,0,13" Name="listBox1" HorizontalAlignment="Left"         VerticalAlignment="Top" Width="194"

WPF中ComboBox用法

The ComboBox control is in many ways like the ListBox control, but takes up a lot less space, because the list of items is hidden when not needed. The ComboBox control is used many places in Windows, but to make sure that everyone knows how it looks

WPF DataTriger 用法示例代码

用法1: <DataGridTemplateColumn Header="{lex:LocText ExamineRoom}"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <TextBlock Name="tb" Text="{Binding ExamineRoom}"> </TextBlock> <DataTempl

WPF的DataGrid用法-小白向

前几天打算尝试下DataGrid的用法,起初以为应该很简单,可后来被各种使用方法和功能实现所折磨.网络上的解决方法太多,但也太杂.没法子,我只好硬着头皮阅览各种文献资料,然后不断的去尝试,总算小有成果.因此,把我学到的和大家分享一下,相信这篇文章会让你再很短的时间内学会DataGrid的大部分主要功能,而且很多难点都可以在里面找到解决方案. 由于涉及的应用比较多,所以篇幅会很长.但可以确保各个版块相互独立,总共4个部分 1.数据绑定 2.DataGrid的增改删功能 3.DataGrid的分页实