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: