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-in-one column data), and its child elements cannot be inherited and used.
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. The Code is as 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 very diligent"}, new StuModel () {StuName = "Li Si", StuAge = 14, stuInfo = "He is very lazy"}, new StuModel () {StuName = "Wang Wu", StuAge = 16, StuInfo = "he is very 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 "placement =" Left "/> <ListBox Height =" 239 "HorizontalAlignment =" Left "Name =" lbDetail "VerticalAlignment = ""Top" Width = "343" ItemsSource = "{Binding StuList}" SelectedValuePath = "StuAge" DisplayMemberPath = "StuName" Margin = "27,61, 0, 0 "/> </Grid> </UserControl>
Finally, let's look at another way to bind DataContext to the background.
Public partial class MainPage: UserControl {public MainPage () {InitializeComponent (); // Method 2: bind DataContext // this with background code. layoutRoot. dataContext = new ClaModel ();}}
To download the source code, click SLDataContext.zip to download it, as shown below:
650) this. width = 650; "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1K3201305-0.jpg "data-mce-src =" http://www.bkjia.com/uploads/allimg/131228/1K3201305-0.jpg "/>
This article is from the "Cheng Xingliang-Silverlight" blog, please be sure to keep this source http://chengxingliang.blog.51cto.com/3972944/841981