What is the meaning of data binding?
The ability to reduce the coupling between background fetch data and XAML presentation page elements.
There are several forms of data binding, and the next one by one statements.
Demo One
First, a single data binding: In the background to define a common level of automatic properties, and then the XAML initialization statement before execution of the property assignment, the XAML initialization after execution, the current data into the data context, and then in the foreground page can be data binding to get the current data, the code is as follows:
1 PublicDateTime Time {Get;Set; }2 PublicMainPage ()3 {4 //Initializing Data5Time =DateTime.Now;6 This. InitializeComponent ();7 //Be sure to place data bindings behind XAML initialization8 This. DataContext =Data;9}
Front Code:
<x:name= "txt" Text= "{Binding}/>
To start debugging, we can see that the current time is bound to the textbox. What if I now have a lot of properties to bind to the foreground element?
Demo
First, a ViewModel class is used to define the properties of various displays, and if our front desk needs to display a variety of people's information, including name, age, height, first define a Personviewmodel class (this is a data source),
1 Public classPersonviewmodel2{//Person Data Source3 Public stringName {Get;Set; }4 Public intAge {Get;Set; }5 Public floatHeight {Get;Set; }6}
Then define a property to accept the data,
Public Get set; }
It is then initialized in the constructor,
1 // Initialize data 2 persondata = new personviewmodel {Name = " , age = 24 , Height = 175 }; 3 this . InitializeComponent (); 4 number It is bound to the data context must be placed after the XAML initialization statement 5 this. DataContext = persondata;
Finally, data binding is done in the foreground,
1<StackPanel>2<TextBoxx:name= "Txtname"Header= "Name"Text="{Binding Name}"/>3<!-here {binding Name} equals {binding path=name}-->4<TextBoxx:name= "Txtage"Header= "Age"Text="{Binding Age}"/>5<!-If the property of the binding has a sub-attribute (that is, a class is bound) then you can use "." If you want to get the property below the property. Point out-->6<TextBoxx:name= "TxtHeight"Header= "height"Text="{Binding Height}"/>7<!-If the binding property is a collection, you can also use the indexer's Form property [index] for data binding-->8</StackPanel>
Running results such as:
All of the above two demos are assigned data to the data context DataContext in the form of a background assignment, and we can also use the DataContext property for data binding on the foreground page.
Demo Three
The background of the
this. DataContext = Persondata;
Statement is commented out, this time debugging person's information is certainly not displayed, because our foreground binding data needs to get data from the data context DataContext, when we add a DataContext property in the page section
datacontext= "{Binding persondata,relativesource={relativesource mode=self}
, you can assign a value to DataContext in the form of an attribute, and if you want to verify that the data source is correct, just press F12 on Persondata to go to the definition, if you go to a background data source that corresponds to an automatic property
Public Get set; }
, then it means that DataContext succeeded in assigning a value. Binding the DataContext data context with foreground properties Note: In the background must first initialize the data, and then initialize the XAML, you think Ah, the XAML is initialized after you give DataContext assignment, is not late.
This form of binding data can be completely separated from the front and back, I only need to set up the data source in the background, and then the data binding work is completely in the foreground, regardless of whether the foreground element is a TextBox or other controls are not related to the background. In this way, the development of Code Division is clear, flexibility is also high.
Have you ever come up with a doubt that I put DataContext artistic form in the page section why is the element below the page section bound to the data? This is because the data context DataContext can be inherited, as long as the parent element sets the data context, its child elements are able to use the data context, and of course their own elements can bind their own or rewrite the parent's DataContext (DataContext has a proximity principle).
It's all about binding data using a custom data source, and we can also use UI elements for data binding, which can also enable interaction between two UI elements.
Demo Four
1 <StackPanel>2 <Sliderx:name= "Slider"Minimum= "0"Maximum= "+"Value= "Ten"/>3 <RectangleWidth="{Binding Elementname=slider,path=value}"Height="{Binding Elementname=slider,path=value}"Fill= "Green"/>4 </StackPanel>
Define a slider slider to control the size of the rectangle by the slider, just bind the width and Height properties of the rectangle, and bind the control to the control by assigning the value change attribute of the elementname to the controller Name,path. Implement the interaction.
When the data source and target properties are of different types, for example, the foreground needs a string type of data, and I now have an int type of data, then the type conversion is required. To implement a type conversion, you can create a new conversion factory, he must implement the IValueConverter interface, under the Convert method you can write the conversion code (Convertback is also used to write the target data method, this method is applicable to TwoWay binding mode).
Here again talk about the binding mode, the above several demo with the binding mode is default is bound mode, binding mode is onetime, OneWay, TwoWay three modes, the following with a demo to explain the difference between the three binding modes.
Demo Five
When we slide the slider, we will find that only the value of onetime is not changed, the value of the slider is set at the beginning of how much he is, and in the TwoWay text box to enter a value will cause the slider, Normal, oneway values also change.
Front Source:
1 <Gridx:name= "LayoutRoot">2 3 <grid.rowdefinitions>4 <RowDefinitionHeight= "Auto"/>5 <RowDefinitionHeight="*"/>6 </grid.rowdefinitions>7 8 <!--Title Panel -9 <StackPanelGrid.Row= "0"Margin= "19,0,0,0">Ten <TextBlockText= "Binding Mode"Style="{ThemeResource Titletextblockstyle}"Margin= "0,12,0,0"/> One <TextBlockText= "Binding Mode"Margin= "0,-6.5,0,26.5"Style="{ThemeResource Headertextblockstyle}"characterspacing="{ThemeResource pivotheaderitemcharacterspacing}"/> A </StackPanel> - - <!--TODO: The content should be placed in the following grid - the <StackPanelGrid.Row= "1"x:name= "Contentroot"Margin= "19,9.5,19,0"> - <Sliderx:name= "Slider"Header= "Data source"Grid.Row= "0"/> - <TextBoxHeader= "Normal"Text="{Binding Elementname=slider,path=value}"/> - <TextBoxHeader= "OneTime"Text="{Binding Elementname=slider,path=value,mode=onetime}"/> + <TextBoxHeader= "OneWay"Text="{Binding Elementname=slider,path=value,mode=oneway}"/> - <TextBoxHeader= "TwoWay"Text="{Binding Elementname=slider,path=value,mode=twoway}"/> + </StackPanel> A </Grid>
Windows Phone 8.1 Data binding