Silverlight databinding binding details (Study Notes) -- reprinted

Source: Internet
Author: User
Original article address: the use of controls such as render textbox is also easy to understand, but Silverlight still follows. NET provides the data binding function. Using Data Binding can make Silverlight data operations more flexible,

When using Silverlight for development, you will feel that data operations are very simple. Whether it is using WCF or WebClient, It is very convenient for Silverlight to process and display data after server communication, textblock textbox and other controls are easy to use, but Silverlight still follows. NET provides the data binding function. Using Data Binding can make Silverlight data operations more flexible and orderly.

Developed ASP. net knows how often and important data binding is. Data is automatically updated on pages, and different user operations display different data, which can be said to be the basis of web development, data Binding in Silverlight is also very important.

The Silverlight document provided by Microsoft details various data binding methods. However, since the binding in Silverlight is a little different from the Data Binding in ASP. NET, the developer who just came into contact with Silverlight may feel unknown after reading the document. The following describes how to use a common method.

First, Silverlight binds data to the class binding, which provides a wealth of methods and attributes.

The specific member list and description can be found in the Silverlight document.

The binding class provides three onetime oneway twoway attributes, which can specify how the data source interacts with the target. And one-time binding. The target changes with the data source, and the target and data source change simultaneously.

The binding class is not very complex. It can be said that it is an intermediate coordinator between the data source and the target. Data Binding will certainly be used. The specific description can be found in the Silverlight document.

Data Binding can be performed through XAML, for example: (Microsoft example)

Create a binding in XAML

1. Define the source object.

C #

1234
public class Dog{    public string DogName { get; set; }}

2. Create a reference to the namespace of the source object in XAML.

XAML

1234
UserControl x:Class="BindingXAML.Page"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:my="clr-namespace:BindingXAML">

3. Create an instance of the source object in resources.

XAML

123
<Grid.Resources>    <my:Dog x:Name="MyDog" DogName="Spot"/></Grid.Resources>

4. Bind The Source attribute or datacontext attribute to the source object. All child levels of this Element Inherit datacontext.

XAML

<Textblock text = "{binding dogname, source = {staticresource mydog}, mode = onetime}"/>

-Or-

XAML

<Textblock text = "{binding dogname, mode = onetime}" datacontext = "{staticresource mydog}"/>

In this example, the data source is a XAML object, which is an instance of the mydog class created in the code. Binding is the binding specified in textblock in XAML. If it is specified as the code in the example, it is equivalent to creating a binding object and specifying its path (dogname) and mode.

Use code to create a binding

1. Add the system. Windows. Data namespace.

C #

Using system. Windows. data; (binding class namespace)

2. Define the source object.

C #

Public class dog

{

Public String dogname {Get; set ;}

}

3. Create the frameworkelement to be bound.

XAML

<Grid X: Name = "layoutroot" background = "white">
   <Textblock X: Name = "mytextblock" text = "test"/>
</GRID>

4. Create an instance of the source object.

C #

Dog mydog = new dog ();

Mydog. dogname = "spot ";

5. Create a bound object.

C #

Binding mybinding = new binding ();

6. Set the binding property for the bound object.

C #

Mybinding. Path = new propertypath ("dogname ");

Mybinding. mode = bindingmode. Onetime;

7. Set the source attribute or datacontext attribute to bind the source. All child levels of this Element Inherit datacontext.

C #

Mybinding. Source = mydog;

-Or-

C #

Mytextblock. datacontext = mydog;

8. Attach this binding to the properties of frameworkelement.

C #

Mytextblock. setbinding (textblock. textproperty, mybinding );

The above example shows how to create a binding using code. In this example, the binding object is not created in the text attribute of textblock. Instead, the binding object is created in the background and the attributes are set. setbinding (textblock. textproperty, mybinding); binds the text property.

In both examples, we can see that there are two methods to specify the data source. For example, textblock can be used to specify the source attribute of the binding class to the data source, or you can specify the datacontext of textblock to the data source, the effect is the same. If textblock does not specify a data source, it searches for the data source in the binding.

In actual development, to make the program more organized, we will use two methods in combination, create a data source in the background, and bind the data source in the XAML. In this way, the front-end eliminates the need to create a data source's XAML element. The back-end eliminates the need to create binding class code. by viewing the front-end's XAML code, you can easily determine the bound data of each control.

Code:

  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99100101102103104105106107108109110111112113114115116117118119120121122
Using system; using system. collections. generic; using system. LINQ; using system. net; using system. windows; using system. windows. controls; using system. windows. documents; using system. windows. input; using system. windows. media; using system. windows. media. animation; using system. windows. shapes; using system. windows. data; namespace silverlighttest {public partial class databind: usercontrol {public databind () {initializecomponent ();} private void usercontrol_loaded (Object sender, routedeventargs E) {mind M = new mind (); txt1.datacontext = m ;}} public class mind // data source {string _ INFO = "OK "; public String info {get {return _ INFO;} set {_ INFO = value ;}}} XAML: <usercontrol xmlns: Data = "CLR-namespace: system. windows. controls; Assembly = system. windows. controls. data "X: class =" silverlighttest. databind "xmlns =" http://schemas.microsoft.com/winfx/2006/xaml/presentation "xmlns: x =" http://schemas.microsoft.com/winfx/2006/xaml "width =" 400 "Height =" 300 "loaded =" usercontrol_loaded "> <grid X: name = "layoutroot" background = "white"> <textblock X: Name = "txt1" text = "{binding info, mode = onetime} "/> </GRID> </usercontrol>

This method makes data binding more readable, easier to understand, and the most common method.

The following compares the XAML creation and code creation:

{Binding info, mode = onetime}

Equivalent

Binding mybinding = new binding ();

Mybinding. Path = new propertypath ("info ");
Mybinding. mode = bindingmode. Onetime;

TEXT = "{binding info, mode = onetime }"

Equivalent

Mytextblock. setbinding (textblock. textproperty, mybinding );

<Textblock text = "{binding dogname, mode = onetime}" datacontext = "{staticresource mydog}"/> datacontext = "{staticresource mydog}

Equivalent

Txt1.datacontext = m;

The above is a brief introduction to Data Binding in Silverlight.

The method of creating data binding between hybrid XAML and code is the easiest to understand and read. Therefore, we often see that after using the DataGrid to define itemssource to the data source, in XAML, "{binding address}" is used directly for Data Binding. itemsource in the DataGrid is equivalent to the datacontext attribute of textblock. The data binding methods of other controls are similar, in addition, data sources can use classes or constant types such as string types. When using constant types, you do not need to specify the path attribute of binding, because it is data.

In addition, there are many advanced usage of binding, such as data sharing by controls and data update binding. These contents are available in the Silverlight document. After you understand the basic concepts, you will not be able to leave them alone.

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.