DataBinding and INotifyPropertyChanged : Data Binding « Data « Silverlight






DataBinding and INotifyPropertyChanged

  

<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'>    
    <StackPanel Background="White">
        <TextBlock Text="Event Address" FontFamily="Verdana" FontSize="24" 
            HorizontalAlignment="Left" Margin="15,0,0,0"/>
            
    <Border BorderBrush="Black"  BorderThickness="1" Margin="15">
    <StackPanel x:Name="AddressGrid">
        <TextBlock Text="Location: " Style="{StaticResource TextBlockPrompt}" />
      <TextBox x:Name="Location" Style="{StaticResource TextBoxStyle}" Text ="{Binding Location, Mode=TwoWay }" /> 
        <TextBlock Text="Address Line 1: " Style="{StaticResource TextBlockPrompt}" />
        <TextBox x:Name="Address1" Style="{StaticResource TextBoxStyle}" Text ="{Binding Address1, Mode=TwoWay }" />
        <TextBlock Text="Address Line 2: " Style="{StaticResource TextBlockPrompt}" />
        <TextBox x:Name="Address2" Style="{StaticResource TextBoxStyle}" Text ="{Binding Address2, Mode=TwoWay }"/>
        <TextBlock Text="City, State, Zip " Style="{StaticResource TextBlockPrompt}" />
        <TextBox x:Name="City" Style="{StaticResource TextBoxStyle}" Text ="{Binding City, Mode=TwoWay }"/>
      </StackPanel>
    </Border>       
  </StackPanel>
</UserControl>

//File:Page.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.ComponentModel;
namespace SilverlightApplication3
{
    public class Address : INotifyPropertyChanged
    {
        private string location;
        private string address1;
        private string address2;
        private string city;
        public event PropertyChangedEventHandler PropertyChanged;
        public string Location
        {
            get { return location; }
            set
            {
                location = value;
                NotifyPropertyChanged("Location");
            }
        }    
        public string Address1
        {
            get { return address1; }
            set
            {
                address1 = value;
                NotifyPropertyChanged("Address1");
            }
        }
        public string Address2
        {
            get { return address2; }
            set
            {
                address2 = value;
                NotifyPropertyChanged("Address2");
            }
        }

        public string City
        {
            get { return city; }
            set
            {
                city = value;
                NotifyPropertyChanged("City");
            }
        }
        public void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this,
                    new PropertyChangedEventArgs(propertyName));
            }

        }
    }
    public partial class MainPage : UserControl
    {
        private Address theAddress;
        public MainPage()
        {
            InitializeComponent();
            theAddress = new Address();
            Loaded += new RoutedEventHandler(Page_Loaded);
        }

        void Page_Loaded(object sender, RoutedEventArgs e)
        {
            AddressGrid.KeyDown += new KeyEventHandler(AddressGrid_KeyDown);
        }

        void AddressGrid_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.C && Keyboard.Modifiers == ModifierKeys.Control)
            {
                theAddress.Location = "address";
                theAddress.Address1 = "Boston!";
                theAddress.Address2 = "1 Blvd";
                theAddress.City = "CA";
            }
            if (e.Key == Key.M && Keyboard.Modifiers == ModifierKeys.Control)
            {
                theAddress.Location = "B";
                theAddress.Address1 = "Way";
                theAddress.Address2 = "Building 10";
                theAddress.City = "WA";
            }
            this.DataContext = theAddress;
        }
    }
}

   
    
  








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.Implementing Data Binding in Silverlight ApplicationsImplementing Data Binding in Silverlight Applications
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