WPF templates fall into two major categories:
1.ControlTemplate: The appearance of the control, that is, what the control looks like.
2.DataTemplate: Is the performance of the data content, a piece of data shows what it looks like.
(1) datatemlate data templates are commonly used in the following places:
1.ContentControl ContentTemplate Property
2.ItemsControl ItemTemplate property. The CellTemplate property of the
3.GridViewColumn.
2. Instance Code
<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= "Width=" "525",
<stackpanel x:name= "StackPanel";
<listbox margin= "Ten" itemssource= "{Binding}"
<listbox.itemtemplate>
<datatemplate;
<stackpanel orientation= "Horizontal";
<textblock text= "{Binding path=name}" margin= "ten" ></TEXTBLOCK>
<textblock text= "{Binding path= Score} "margin=" ></TEXTBLOCK>
<checkbox ischecked= "{Binding path=gender}" margin= "></" 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;}
}
}
The use of DataTemplate