Introducing Two-Way Data Binding using Silverlight 2.0, WCF and LINQ to SQL - Explaining two-way binding
(Page 5 of 5 )
Let us have a look at the textboxes' definitions:
<TextBox Margin="8,8,46,8" Grid.Column="1" Text="{Binding Empno, Mode=TwoWay }" TextWrapping="Wrap" x:Name="txtEmpno" />
<TextBox Margin="8,8,46,8" Text="{Binding Ename, Mode=TwoWay }" TextWrapping="Wrap" Height="23.9" VerticalAlignment="Stretch" Grid.Column="1" Grid.Row="1" x:Name="txtEname"/>
<TextBox Margin="8,8,46,8" Text="{Binding Sal, Mode=TwoWay }" TextWrapping="Wrap" Height="23.9" VerticalAlignment="Stretch" Grid.Column="1" Grid.Row="2" x:Name="txtSal"/>
<TextBox Margin="8,8,46,8" Text="{Binding Deptno, Mode=TwoWay }" TextWrapping="Wrap" Height="23.9" VerticalAlignment="Stretch" Grid.Column="1" Grid.Row="3" x:Name="txtDeptno"/>
The most important property (say attribute) to examine in all of the above textboxes is the "Text" with "Binding" specification. The binding specification includes the word "Binding" followed by a property name and the "Mode," which is "TwoWay."
Once the "Mode" is provided as "TwoWay" and the user has modified any text in the textboxes, the business object (say "Emp" object in this case) automatically reflects the changes. Similarly, if business object values are modified, it would automatically be reflected in the controls.
The business object will generally be assigned to a container control with the help of the "DataContext" property. Once the container control receives the object, it passes the object to all of its child controls and each of the child controls pull whatever information they require (using the Binding specification).
In our case, the DataContext property is set in code-behind as follows:
Private objEmp As EmpService.Emp
Public Sub New()
InitializeComponent()
objEmp = New EmpService.Emp
Me.LayoutRoot.DataContext = objEmp
End Sub
All of the information in "objEmp" is automatically carried to textboxes and vice versa (which is two-way binding).
In my upcoming articles, we will see more and more examples of Silverlight 2.0 development together with LINQ to SQL and WCF. I hope you enjoyed the article; any suggestions, bugs, errors, enhancements etc. are highly appreciated at http://jagchat.spaces.live.com
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |