ARTICLE

Bind Data using DataTempale in Silverlight

Posted by Manish Tewatia Articles | Silverlight using VB.NET October 27, 2010
In this article you will learn how to Bind Data using DataTempale in Silverlight.
 
Reader Level:

HTML clipboard

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"
    xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
    xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
    xmlns:d='http://schemas.microsoft.com/expression/blend/2008'
    xmlns:mc='http://schemas.openxmlformats.org/markup-compatibility/2006'
    mc:Ignorable='d'
    d:DesignWidth='640'
    d:DesignHeight='480'
    xmlns:SilverlightApplication19="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"> 
    <UserControl.Resources>
        <DataTemplate x:Key="Add">
            <StackPanel>
                <TextBlock x:Name="tblkStreet" Text="{Binding Street}"/>
                <StackPanel>
                    <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}"/>
                </StackPanel>
            </StackPanel>
        </DataTemplate>
        <DataTemplate x:Key="Emp">
            <StackPanel>
                <Rectangle HorizontalAlignment="Stretch"/>
                <Rectangle HorizontalAlignment="Stretch" Margin="0,0,0,0"
                   RadiusY="3" Stroke="#FF686868" StrokeThickness="0"
                   Width
="Auto">
                </Rectangle>
                <Rectangle HorizontalAlignment="Stretch" Margin="3,3,3,3"
                   Stroke="#FF0A28EE" Grid.RowSpan="1"
                   StrokeThickness="5" VerticalAlignment
="Stretch"/>
                <StackPanel>
                    <TextBlock x:Name="tblkFirstName" Text="{Binding FirstName}"/>
                    <TextBlock x:Name="tblkLastName" Text="{Binding LastName}"/>
                </StackPanel>
                <StackPanel>
                    <ContentControl ContentTemplate="{StaticResource Add}" Content="{Binding Address}"/>
                    <TextBlock x:Name="tblkPhoneNum" Text="{Binding PhoneNum}"/>
                </StackPanel>
            </StackPanel>
        </DataTemplate>
    </UserControl.Resources> 
    <StackPanel>
        <ListBox x:Name="itmctrlEmployees"
             HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
             ItemTemplate="{StaticResource Emp}"
           
 />
    </StackPanel>
</
UserControl>

 //File: MainPage.xaml.cs

using System.Collections.Generic;
using System.Windows.Controls;
 
namespace SilverlightApplication19
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            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 { get; set; }
    public string LastName { get; set; }
    public long PhoneNum { get; set; }   
    public Address Address { get; set; }
  }
  public class Address
  {
    public string Street { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public int ZipCode { get; set; }       
    }
}

Output Window

Untitled-1.gif
Conclusion

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

Login to add your contents and source code to this article
share this article :
post comment
 
Nevron Diagram
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor