Implementing Data Binding in Silverlight Applications : Data Binding « Data « Silverlight






Implementing Data Binding in Silverlight Applications

Implementing Data Binding in Silverlight Applications
  

<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>
//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");
               }
           }
       }
  }

   
    
  








Related examples in the same category

1.Binding Application Data to the UIBinding Application Data to the UI
2.Receiving Change Notifications for Bound Data
3.Validating Input for Bounded Data
4.DataBinding and INotifyPropertyChanged
5.Bind to a collection
6.Add a value converter to a binding using XAML
7.Without Binding
8.Text Data Binding
9.Bind to an Existing Object InstanceBind to an Existing Object Instance
10.Binding FontFamily / FontSize value for current ControlBinding FontFamily / FontSize value for current Control
11.Binding an Emoticon object to a GridBinding an Emoticon object  to a Grid
12.Binding a collection of Emoticon objects to a ListBox through the ItemsSource propertyBinding a collection of Emoticon objects to a ListBox through the ItemsSource property