Tuesday 15 May 2012

Implementing Data Binding in Silverlight Applications

XAML CODE:

  
<UserControl x:Class='SilverlightApplication3.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'>
     <Grid x:Name="LayoutRoot" Background="LightBlue">
        <TextBlock x:Name="nameTitle"
                      Text="{Binding Name, Mode=OneWay}"
                      FontSize="30"
                      VerticalAlignment="Top"
                      HorizontalAlignment="Left"
                      Margin="10,10,0,0"
                      Height="50" Width="300" />
        <TextBox x:Name="nameBox"
                  Text="{Binding Name, Mode=TwoWay}"
                  VerticalAlignment="Top"
                  HorizontalAlignment="Left"
                  Margin="10,80,0,0"
                  Height="30" Width="300" />
         <ListBox x:Name="numberList"
                  ItemsSource="{Binding Numbers, Mode=OneWay}"
                  VerticalAlignment="Top"
                  HorizontalAlignment="Left"
                  Margin="10,120,0,0"
                  Height="120" Width="300" />
         <Button x:Name="UpdateBtn" Content="Update"
                 VerticalAlignment="Bottom"
                 Margin="-120,0,0,10"
                 Height="30" Width="100" />
         <Button x:Name="SwitchBtn" Content="Switch"
                 VerticalAlignment="Bottom"
                 Margin="120,0,0,10"
                 Height="30" Width="100" />
    </Grid>
</UserControl>


CODE BEHIND FILE


//File:Page.xaml.cs

  using System;
  using System.Collections.Generic;
  using System.Windows;
  using System.Windows.Controls;

  using System.ComponentModel;

  namespace SilverlightApplication3
  {
      public partial class MainPage : UserControl
      {
          Contact personA, personB, current;
          Boolean isCurrentA;
          public MainPage()
          {
              InitializeComponent();

              initData();
              setContext(personA);
              isCurrentA = true;

              UpdateBtn.Click += new RoutedEventHandler(doUpdate);
              SwitchBtn.Click += new RoutedEventHandler(doSwitch);
           }

           void initData()
           {
               personA = new Contact();
               personA.Name = "Mike";
               personA.Numbers =
                  new List<string>() { "111-222-333""444-555-6666" };
               personB = new Contact();
               personB.Name = "Ted";
               personB.Numbers =
                  new List<string>() { "123-456-7890""098-765-4321" };
           }

           void setContext(Contact c)
           {
              current = c;
              LayoutRoot.DataContext = c;
           }

           void doUpdate(object sender, RoutedEventArgs e)
           {
              current.Name = nameBox.Text;
           }

           void doSwitch(object sender, RoutedEventArgs e)
           {
               if (isCurrentA)
               {
                  setContext(personB);
                  isCurrentA = false;
               }

              else
              {
                 setContext(personA);
                 isCurrentA = true;
              }
           }
       }

       public class Contact : INotifyPropertyChanged
       {
           private string ContactName;
           private List<string> ContactNumbers;
           public event PropertyChangedEventHandler PropertyChanged;

           public Contact()
           {
           }

           public void NotifyPropertyChanged(string property)
           {
              if (PropertyChanged != null)
              {
                  PropertyChanged(this,
                                  new PropertyChangedEventArgs(property));
               }
           }

           public string Name
           {
               get return ContactName; }
               set
               {
                   ContactName = value;
                   NotifyPropertyChanged("Name");
               }
           }

           public List<string> Numbers
           {
               get return ContactNumbers; }
               set
               {
                   ContactNumbers = value;
                   NotifyPropertyChanged("Numbers");
               }
           }
       }
  }



FRNDZ PLEASE POST YOUR COMMENTS SO THAT THE ARTICLE MAY BE ENHANCED
   

No comments:

Post a Comment