Silverlight Bind Data using DataTempale in VB.NET

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

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="{BindingAddress}"/>
                    <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 { 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

Untitled-1.gif
Conclusion

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

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.