title: How do I access XAML bound class properties in code? (C#, WPF form)
body: <p>In the example below, I am using XAML to set the DataContext to a class name, not an object name (as far as I can tell). The binding works - the txtEcho textbox shows what is entered in the txtName textbox when the latter loses focus. I want to be able to access the properties of this class in code-behind so I need an object reference. I could create a Person object and set the DataContext to that  but the pattern in the books I am consulting does not do that - the class name is used and an object is not explicitly created in the examples in these textbooks.</p>
  
  <pre><code>&lt;Window x:Class="WpfApplication5.MainWindow"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:local="clr-namespace:WpfApplication5"
          Title="MainWindow" Height="350" Width="525"&gt;
      &lt;Window.Resources&gt;
          &lt;local:Person x:Key="personData" /&gt;
      &lt;/Window.Resources&gt;
      &lt;Grid&gt;
          &lt;Grid.DataContext&gt;
              &lt;Binding Source="{StaticResource personData}" /&gt;
          &lt;/Grid.DataContext&gt;
          &lt;TextBlock HorizontalAlignment="Left" Margin="27,41,0,0" TextWrapping="Wrap" Text="Name" VerticalAlignment="Top" Height="23"/&gt;
          &lt;TextBox x:Name="txtName" Text="{Binding Name}" HorizontalAlignment="Left" Height="23" Margin="78,42,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/&gt;
          &lt;TextBox x:Name="txtEcho" Text="{Binding Name}" HorizontalAlignment="Left" Height="23" Margin="262,42,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/&gt;
  
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>The code is</p>
  
  <pre><code>namespace WpfApplication5
  {
      /// &lt;summary&gt;
      /// Interaction logic for MainWindow.xaml
      /// &lt;/summary&gt;
      public partial class MainWindow : Window
      {
          public MainWindow()
          {
              InitializeComponent();
          }
  
          private void Button_Click(object sender, RoutedEventArgs e)
          {
              //txtFreedom.Text = &lt;what exactly?&gt;.Name; // no object reference!
          }
      }
      class Person : INotifyPropertyChanged
      {
          string name;
          public string Name
          {
              get { return this.name; }
              set
              {
                  this.name = value;
                  OnPropertyChanged("Name");
              }
          }
          public event PropertyChangedEventHandler PropertyChanged;
          public void OnPropertyChanged(string propertyName)
          {
              if (this.PropertyChanged != null)
                  this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
          }
      }
  }
  </code></pre>