Msdn keypoint in Silverlight Part 1 (datacontext)

Source: Internet
Author: User

What Bill said?

 

Dependency Property Identifier Field: datacontextproperty

"Data context" is a concept that allows objects to inherit the specified information from their parent level in the object tree.

The most important aspect of data context is the data source used for binding. The most common purpose of datacontext is to directly set it as a CLR data source object. If the CLR data source is a static starting dataset, it may be a class. Alternatively, you can create a local data source as the new observablecollection <t>, and then update the collection through the WCF Data Services-WCF data service or other technologies. For examples of the WCF Data Services-WCF data service method, see WCF Data Service (Silverlight ).

The data context may also contain other binding features except the data source, such as the path to the data source. For example, you can create the following object tree in XAML.

[XAML]

 

<StackPanel>  <StackPanel.Resources>    <SolidColorBrush Color="Orange" x:Key="MyBrush"/>  </StackPanel.Resources>  <StackPanel DataContext="{StaticResource MyBrush}">    <Rectangle Height="50" Width="50" Fill="{Binding}" />  </StackPanel></StackPanel>

In this case, the datacontext defined by stackpanel is passed on to the rectangle sub-object and becomes the data context of different unqualified {binding} statements in the fill attribute.

Datacontext is used to facilitate binding to the shared data context, which is usually useful for avoiding detailed binding statements. However, this is valid for undefined datacontext, and it is also valid for all necessary binding limitations that exist in a separate binding statement.

In the code, you can set the data context to a CLR object directly and calculate the binding as the property of the object.

You can also set datacontext to a custom object that is instantiated as a XAML object element in resourcedictionary. You can use staticresource to retrieve the resource and reference the custom object based on the X: key value of the resource.

Datacontext is an attribute that can be bound to implement a scheme where context can be bound to other contexts, although this scheme is not common.

Example

 

The following example shows the UI context of ListBox (named mybooks) and its items. Then, the Code executed during loading hides the datacontext setting on mybooks ListBox. The {binding ISBN} and {binding title} path expressions in The ListBox item inherit datacontext and can be used to bind it to the source, and then use the ListBox item filling behavior to bind it to the data items in the Set combination source.

XAML
<StackPanel>    <ListBox x:Name="MyBooks" Margin="5" ItemsSource="{Binding Mode=OneWay}" >        <ListBox.ItemTemplate>            <DataTemplate>                <StackPanel Orientation="Horizontal" >                    <TextBlock Text="{Binding ISBN}" Margin="0,0,50,0" />            <TextBlock Text="{Binding Title}" />            </StackPanel>            </DataTemplate>        </ListBox.ItemTemplate>    </ListBox>

Vbc # C ++ F # JScript
       //You can add items to your collection       AllBooks.Add(new Book("4458907683", "Training Your Dog",           new DateTime(2000, 2, 8), 44.25));       AllBooks.Add(new Book("0446675385", "Good Owners, Great Dogs",           new DateTime(1999, 9, 1), 15.99));       //Set the data context for the list of books       MyBooks.DataContext = AllBooks;

 

This is just a short example. To view the same sample code that supports Code such as binding source, see how to bind to hierarchical data and create a Master/Slave view. For more information about the datacontext role in the Data Binding scheme, see data binding.

 

 

What actually works?

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.