Data templates in WPF (DataTemplate) (GO)

Source: Internet
Author: User

Original Address http://www.cnblogs.com/zhouyinhui/archive/2007/03/30/694388.html

data templates in WPF (DataTemplate)
Zhou Banhui

In WPF we can customize the display for our own data, that is, although some data data is certain, but we can do it in a variety of ways, such as a time, in the past we generally use a string (such as "12:03") to display, but why we can not display one hours clock , it makes sense to use data templating technology in WPF to freely and easily represent your data.

Data templates apply to the content control class controls and the items control class controls.

Let's say we have one of the following classes

Using System;

Namespace Demo
{
public class people
{
private string name;

private string photo;

Public people (string name, string photo)
{
THIS.name = name;
This.photo = photo;
}

public string Name
{
Get
{
return this.name;
}
Set
{
THIS.name = value;
}
}

public string Photo
{
             get
            {
                 return this.photo;
            }
            set
             {
                 this.photo =  value;
            }
        }
    }
}

This class is simply a representation of a person, his name and his picture (path)

If you have a list control listbox in our software that displays a list of people, before. NET 3.0, we might be able to use text to list the names of people, or spend a lot of effort rewriting the list control to display the photos in the list while the names are displayed.

Refer to the following code:

<listbox x:name= "Listbox_peoplelist" itemtemplate= "{StaticResource MyTemplate}"/>

We defined a ListBox and made its ItemTemplate as our custom mytemplate, which means that the list item will display the list content in the way MyTemplate has developed.
So that we can play our imaginations, from defining mytemplate
In order to be able to use our people class in XAML, we need to introduce its namespace, referring to the following code:

xmlns:demo= "Clr-namespace:demo"

The demo is the name space of our people class, which can be used to represent the namespace later.

The following code defines our MyTemplate template so that we can tell our list how to display his project:

<Window.Resources>
<!--list template--
<datatemplate x:key= "MyTemplate" datatype= "{x:type demo:people}" >
<grid verticalalignment= "center" horizontalalignment= "center" margin= "4,4,4,4" >
<Grid.ColumnDefinitions>
<columndefinition width= "Auto"/>
<columndefinition width= "Auto"/>
</Grid.ColumnDefinitions>
<image source= "{Binding Photo}" width= "height=" "grid.column=" 0 "grid.rowspan=" 1 "/>
<textblock text= "{Binding Name}" grid.column= "1" grid.columnspan= "1" horizontalalignment= "Center" verticalalignment= "Center"/>
</Grid>
</DataTemplate>
</Window.Resources>

We define the template as a window resource, the resource is saved in a resource dictionary, x:key= "MyTemplate" represents its key in the resource dictionary,DataType= "{x:type demo:people}" Indicates that the data template for the data type is the People class under the demo namespace, and then in Gird we define the visual tree of the data template, which is the focus of our work, that is, the visual tree defines how to display our data. We use an image control and bind its source to the photo property of people so that the photo is displayed on the Imag control. Then on the right side of the image we use a TextBlock control to display the name of the person (bind people's Name property to the TextBlock's Text property).
Notice what this data template actually does: it defines how the people type object behaves, here is the photo showing people and the name on the right of the photo.
In the future we need the people object to be presented to the user in this way, as long as we assign the data template to the control that will display the people object.
Like what

<listbox x:name= "Listbox_peoplelist" itemtemplate= "{StaticResource MyTemplate}"/>

Just tell us. The list control displays its items in a manner defined by MyTemplate.

Hehe, this is more convenient than the previous code way to create a personality list control.

Data templates in WPF (DataTemplate) (GO)

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.