In Silverlight, we often see that the data binding source is specified by datacontext or itemssource. What are the differences between them?
Datacontext: Data context is defined by English letters. datacontext is set for a parent element in Silverlight, and its child elements can inherit from and use attributes in datacontext. Data sources bound to datacontext can be object sets, objects, and attributes.
Itemssource: Its data source is usually an entity set (multiple columns of data) and cannot be inherited and used by its child elements.
The following example describes how to use datacontext. In the parent-level element grid, A datacontext attribute is set to bind an object, which contains a set of attributes, this object set is used to bind the itemssource attribute of The ListBox sub-element of the grid.CodeAs follows:
First, let's take a look at two entities. One of them is a list <>:
Public Class Clamodel { Public Clamodel () {claname = " Class 3, grade 5 " ; Clainfo = " The best class in the school, won multiple honors " ; Stulist = New List <stumodel> (){ New Stumodel () {stuname = " Liu san " , Stuage = 15 , Stuinfo =" He is diligent " }, New Stumodel () {stuname = " Li Si " , Stuage = 14 , Stuinfo = " He is very lazy. " }, New Stumodel () {stuname = " Wang Wu " , Stuage = 16 , Stuinfo = " He is smart. " }};} Public String Claname { Get ; Set ;} Public String Clainfo { Get ; Set ;} Public List <stumodel> stulist { Get ; Set ;}} Public Class Stumodel { Public String Stuname { Get ; Set ;} Public Int Stuage { Get ; Set ;} Public String Stuinfo { Get ; Set ;}}
Next, let's look at the following code binding in XAML:
<Usercontrol X: class = " Sldatacontext. mainpage " Xmlns = " Http://schemas.microsoft.com/winfx/2006/xaml/presentation " Xmlns: x = " Http://schemas.microsoft.com/winfx/2006/xaml " Xmlns: d = " Http://schemas.microsoft.com/expression/blend/2008 " Xmlns: MC = " Http://schemas.openxmlformats.org/markup-compatibility/2006 " Xmlns: Local = " CLR-namespace: sldatacontext " MC: ignorable = " D " D: designheight = " 300 " D: designwidth = " 400 " > <! -- Method 1 --> <usercontrol. Resources> <local: clamodel X: Key = " Clasource " /> </Usercontrol. Resources> <grid X: Name = " Layoutroot " Background = " White " Datacontext = " {Staticresource clasource} " > <Textblock fontweight =" Bold " Width = " 60 " Height = " 30 " TEXT = " {Binding claname} " Verticalalignment = " Top " Horizontalalignment = " Left " /> <Textblock fontstyle = " Italic " Width = " 160 " Height = " 30 " TEXT = " {Binding clainfo} " Verticalalignment = " Top " Margin = " 70 0 0 0 " Horizontalalignment = " Left " /> <ListBox Height = " 239 " Horizontalalignment = " Left " Name = " Lbdetail " Verticalalignment = " Top " Width = " 343 " Itemssource = " {Binding stulist} " Selectedvaluepath =" Stuage " Displaymemberpath = " Stuname " Margin = " 27,61 " /> </GRID> </usercontrol>
Finally, let's look at another way to bind datacontext to the background.
Public Partial ClassMainpage: usercontrol {PublicMainpage () {initializecomponent ();//Method 2: You can also bind datacontext with the background code.//This. layoutroot. datacontext = new clamodel ();}}
To download the source code, click sldatacontext.zip to download it, as shown below: