Silverlight Bind Data using DataTempale in VB.NET

In this article you will learn how to Bind Data using DataTempale in Silverlight.
  • 1494

DataBinding: Databinding is the magic that that sits between your objects (know as a model) and the UI (known as a view). The model isn't concerned with how the data will appear in front of the user â€" it's job is to hold the data. Silverlight data binding features are flexible and powerful, and provide you with everything you need to build an effective business application.

Example of Binding

<UserControl x:Class="SilverlightApplication19.MainPage"
        <DataTemplate x:Key="Add">
                <TextBlock x:Name="tblkStreet" Text="{Binding Street}"/>
                    <TextBlock  x:Name="tblkCity" Text="{Binding City}"/>
                    <TextBlock  x:Name="tblkComma" Text=","/>
                    <TextBlock  x:Name="tblkState" Text="{Binding State}"/>
                    <TextBlock  x:Name="tblkZip" Text="{Binding ZipCode}"/>
        <DataTemplate x:Key="Emp">
                <Rectangle HorizontalAlignment="Stretch"/>
                <Rectangle HorizontalAlignment="Stretch" Margin="0,0,0,0" 
                   RadiusY="3" Stroke="#FF686868" StrokeThickness="0" 
                <Rectangle HorizontalAlignment="Stretch" Margin="3,3,3,3" 
                   Stroke="#FF0A28EE" Grid.RowSpan="1" 
                   StrokeThickness="5" VerticalAlignment
                    <TextBlock x:Name="tblkFirstName" Text="{Binding FirstName}"/>
                    <TextBlock x:Name="tblkLastName" Text="{Binding LastName}"/>
                    <ContentControl ContentTemplate="{StaticResource Add}" Content="{BindingAddress}"/>
                    <TextBlock x:Name="tblkPhoneNum" Text="{Binding PhoneNum}"/>
        <ListBox x:Name="itmctrlEmployees" 
             HorizontalAlignment="Stretch" VerticalAlignment="Stretch" 
             ItemTemplate="{StaticResource Emp}" 

 //File: MainPage.xaml.cs

using System.Collections.Generic;
using System.Windows.Controls;
namespace SilverlightApplication19
    public partial class MainPage : UserControl
        public MainPage()
            List<Employee> EmployeeList = new List<Employee>(); 
      EmployeeList.Add(new Employee
            FirstName = "Manish",
            LastName = "Will",
            PhoneNum = 2,
            Address = new Address { Street = "Street 5", City = "Delhi", State = "ND", ZipCode = 10016 }
      EmployeeList.Add(new Employee{
            FirstName = "Jam",
            LastName = "Cordor",
            PhoneNum = 7,
            Address = new Address { Street = "Street 8", City = "Delhi", State = "ND", ZipCode = 10016 }
      itmctrlEmployees.ItemsSource = EmployeeList;
  public class Employee
    public string FirstName { getset; }
    public string LastName { getset; }
    public long PhoneNum { getset; }    
    public Address Address { getset; }
  public class Address
    public string Street { getset; }
    public string City { getset; }
    public string State { getset; }
    public int ZipCode { getset; }        

Output Window


Hope this article would help you to understand how to Bind Data using DataTempale in Silverlight.


More Articles

© 2020 DotNetHeaven. All rights reserved.