title: How to get WPF DataBinding-to-an-object to work
body: <p>In the following example I bind the XAML to a static object via ObjectDataProvider.
  When the user changes information, I want it to automatically reflect in the XAML.</p>
  
  <p>What I don't understand is:</p>
  
  <ul>
  <li>how do I perpetuate the object? do I have to create a singleton? in the click event, how do I access the "object that is being edited"</li>
  <li>eventually of course I want the data to be retrieved from a model which reads an XML file or web service, and <strong>I want of course my ViewModel to check my model every second or so to see if the data has changed and reflect this on the XAML</strong>.</li>
  </ul>
  
  <p>How to I get THERE from HERE:</p>
  
  <p><strong>XAML:</strong></p>
  
  <pre><code>&lt;Window x:Class="TestBinding99382.Window1"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:local="clr-namespace:TestBinding99382"
      Title="Window1" Height="300" Width="300"&gt;
  
      &lt;Window.Resources&gt;
          &lt;ObjectDataProvider 
               x:Key="DataSourceCustomer" 
               ObjectType="{x:Type local:Customer}" MethodName="GetCustomer"/&gt;
  
          &lt;Style x:Key="DataRowStyle" TargetType="StackPanel"&gt;
              &lt;Setter Property="Orientation" Value="Horizontal"/&gt;
              &lt;Setter Property="VerticalAlignment" Value="Top"/&gt;
              &lt;Setter Property="Margin" Value="0 10 0 0"/&gt;
              &lt;Setter Property="DataContext" 
                      Value="{StaticResource DataSourceCustomer}"/&gt;
              &lt;Setter Property="DockPanel.Dock" Value="Top"/&gt;
          &lt;/Style&gt;
      &lt;/Window.Resources&gt;
  
      &lt;DockPanel&gt;
          &lt;StackPanel DockPanel.Dock="Top" 
                      DataContext="{StaticResource DataSourceCustomer}" 
                      Orientation="Horizontal"&gt;
              &lt;TextBlock Text="{Binding Path=FirstName}"/&gt;
              &lt;TextBlock Text=" "/&gt;
              &lt;TextBlock Text="{Binding Path=LastName}"/&gt;
              &lt;TextBlock Text=" ("/&gt;
              &lt;TextBlock Text="{Binding Path=FullName}" FontWeight="Bold"/&gt;
              &lt;TextBlock Text=")"/&gt;
          &lt;/StackPanel&gt;
  
          &lt;StackPanel Style="{StaticResource DataRowStyle}"&gt;
              &lt;TextBlock Text="First Name:"/&gt;
              &lt;TextBox Text="{Binding Path=FirstName}" 
                        Width="200" Margin="3 0 0 0"/&gt;
          &lt;/StackPanel&gt;
  
          &lt;StackPanel Style="{StaticResource DataRowStyle}"&gt;
              &lt;TextBlock Text="Last Name:"/&gt;
              &lt;TextBox Text="{Binding Path=LastName}" 
                       Width="200" Margin="3 0 0 0"/&gt;
          &lt;/StackPanel&gt;
  
          &lt;StackPanel Style="{StaticResource DataRowStyle}"&gt;
              &lt;Button Content="Save Changes" Click="Button_Click"/&gt;
          &lt;/StackPanel&gt;
  
      &lt;/DockPanel&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p><strong>Code Behind:</strong></p>
  
  <pre><code>using System.Windows;
  using System.ComponentModel;
  using System;
  
  namespace TestBinding99382
  {
      public partial class Window1 : Window
      {
          private Customer _customer;
  
          public Window1()
          {
              InitializeComponent();
          }
  
          private void Button_Click(object sender, RoutedEventArgs e)
          {
              //I want to edit the _customer object here
              //and have it my changes automatically reflect in my XAML
              //via the INotifyPropertyChanged inheritance.
          }
      }
  
      public class Customer : INotifyPropertyChanged
      {
          private string _firstName;
          private string _lastName;
  
          public string FirstName
          {
              get
              {
                  return _firstName;
              }
  
              set
              {
                  _firstName = value;
                  this.RaisePropertyChanged("FirstName");
                  this.RaisePropertyChanged("FullName");
              }
          }
  
          public string LastName
          {
              get
              {
                  return _lastName;
              }
  
              set
              {
                  _lastName = value;
                  this.RaisePropertyChanged("LastName");
                  this.RaisePropertyChanged("FullName");
              }
  
          }
  
          public string FullName
          {
              get
              {
                  return String.Format("{0} {1}", _firstName, _lastName);
              }
          }
  
          public event PropertyChangedEventHandler PropertyChanged;
          private void RaisePropertyChanged(string property)
          {
              if (PropertyChanged != null)
              {
                  PropertyChanged(this, new PropertyChangedEventArgs(property));
              }
          }
  
  
          public static Customer GetCustomer()
          {
              return new Customer { FirstName = "Jim", LastName = "Smith" };
          }
  
      }
  }
  </code></pre>