Msdn tutorial short film WPF 19 (BIND 2-bind resource template)

Source: Internet
Author: User
Bind resource template

In the previous section, we learned how to bind the resource template. Next we will learn about how to bind the resource template.

First, we define a person class (person. CS). For example, if the person name attribute exists, the change notification can be implemented. (Person. CS)

 Code  Public   Class Person: inotifypropertychanged { Private   String Name; Public   Event Propertychangedeventhandler propertychanged; Public Person (){} Public Person ( String  Value ){ This . Name = Value ;} Public   String Personname { Get { Return Name ;} Set {Name = Value ; Onpropertychanged (" Personname ");}} Protected  Void Onpropertychanged ( String Name) {propertychangedeventhandler handler = propertychanged; If (Handler! = Null ) {Handler ( This , New Propertychangedeventargs (name ));}}

Let's give it some static data.

First, create a. CS file named datas. CS. Which is:

CodePublic ClassDatas: List <person> {PublicDatas () {Add (NewPerson ("Zhang San"); Add (NewPerson ("Li Si"); Add (NewPerson ("Wang Wu"));}}

The background is ready. Let's start the foreground.

Add

Xmlns: Local = "CLR-namespace: Demo"

The demo here is the project name, that is, the namespace name. This is used to reference the two class files we just created.

See the following XAMLCode.

Code<Grid> <grid. Resources> <local: datas X: Key ="Listboxdata"/> <Datatemplate X: Key ="Listboxdatatemplate"> <Textblock text ="{Binding personname}"/> </Datatemplate> </grid. Resources> <ListBox itemtemplate ="{Staticresource listboxdatatemplate}"Itemssource ="{Staticresource listboxdata}"> </ListBox> </GRID>In the resource, hereListboxdata is the key value referenced below, and datatemplate is the data template.

Datatemplate is used to specify the visualization of data objects. Itemtemplate: gets or sets the datatemplate used to display each item.
Itemssource is a set of items used to generate itemscontrol. Running result:

We can add some styles to him. For example, in datatemplate

 
<Datatemplate X: Key ="Listboxdatatemplate"> <Textblock text ="{Binding personname}"Fontsize ="32"Fontstyle ="Oblique"Foreground ="Blue"/> </Datatemplate>

Result:

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.