Beginning Silverlight 2.0 Development using Visual Studio 2008 - Basic Data Binding in Silverlight 2.0
(Page 5 of 6 )
Let us now deal with data binding using Silverlight 2.0. Create a new Silverlight solution and add a new class (to Silverlight project) as follows:
Public Class Emp
Private _Empno As String
Public Property Empno() As String
Get
Return _Empno
End Get
Set(ByVal value As String)
_Empno = value
End Set
End Property
Private _Ename As String
Public Property Ename() As String
Get
Return _Ename
End Get
Set(ByVal value As String)
_Ename = value
End Set
End Property
End Class
Modify Page.xaml to look as follows:
<UserControl x:Class="TestSilverlight.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300">
<Canvas Margin="30,26,31,34">
<Button Height="23" Width="107" Canvas.Left="43" Canvas.Top="8" Content="Bind Now" x:Name="Bind" Click="btnBind_Click"/>
<TextBlock Height="20" Width="107" Canvas.Left="43" Canvas.Top="51" Text="Empno:" TextWrapping="Wrap"/>
<TextBox Height="20" Width="142" Canvas.Left="170" Canvas.Top="52.269" Text="{Binding Empno}" TextWrapping="Wrap" x:Name="txtEmpno"/>
<TextBlock Height="20" Width="107" Text="Ename:" TextWrapping="Wrap" Canvas.Left="43" Canvas.Top="91.269"/>
<TextBox Height="20" x:Name="txtEname" Width="142" Text="{Binding Ename}" TextWrapping="Wrap" Canvas.Left="170" Canvas.Top="91.269"/>
</Canvas>
</UserControl>
Modify Page.xaml.vb to look as follows:
Partial Public Class Page
Inherits UserControl
Public Sub New()
InitializeComponent()
End Sub
Private Sub btnBind_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
Dim objEmp As New Emp With {.Empno = "1001", .Ename = "Jag"}
Me.txtEmpno.DataContext = objEmp
Me.txtEname.DataContext = objEmp
End Sub
End Class
Once you execute the above, you should have output as shown below (Fig 08):

The next section gives you an understanding of the code above.
Next: Basic Data Binding in Silverlight 2.0: explanation >>
More BrainDump Articles
More By Jagadish Chaterjee