The use of DataTemplate

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.