title: Setting DataContext of elements inside XAML?
body: <p>Hi am trying to get the hang of binding.</p>
  
  <p>The XAML code:</p>
  
  <pre><code>&lt;Window x:Class="WPF_SandBox.MainWindow"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  
          Title="MainWindow" Height="350" Width="525"&gt;
      &lt;StackPanel x:Name="stackPanel"&gt;
          &lt;TextBox x:Name="textBox_FirstName" Width="200" Margin="0,100,0,0" Text="{Binding Path=FirstName, UpdateSourceTrigger=PropertyChanged}"  /&gt;
          &lt;TextBox x:Name="textBox_LastName" Width="200" Margin="0,10,0,0" Text="{Binding Path=LastName, UpdateSourceTrigger=PropertyChanged}" /&gt;
          &lt;TextBlock x:Name="textBlock_FullName"  Background="LightBlue" Width="200" Margin="0,10,0,0" Text="{Binding Path=FullName, UpdateSourceTrigger=PropertyChanged}"  /&gt;
      &lt;/StackPanel&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>The C# code:</p>
  
  <pre><code>public partial class MainWindow : Window
      {
          public MainWindow()
          {
              InitializeComponent();
              Person person = new Person { FirstName = "Matt", LastName = "Smith" };
              stackPanel.DataContext = person;
  
          }
      }
  
      public class Person : INotifyPropertyChanged
      {
          string firstName;
          string lastName;
  
          public string FirstName
          {
              get
              { 
                  return firstName;
              }
              set
              {
                  firstName = value;
                  OnPropertyChanged("FirstName");
                  OnPropertyChanged("FullName");
              }
          }
  
          public string LastName
          {
              get { return lastName; }
              set
              {
                  lastName = value;
                  OnPropertyChanged("LastName");
                  OnPropertyChanged("FullName");
              }
          }
  
          public string FullName
          {
              get
              {
                  return String.Format("{0}, {1}",lastName,firstName);
              }
          }
  
          public event PropertyChangedEventHandler PropertyChanged;
  
          private void OnPropertyChanged(string propName)
          {
              if (PropertyChanged != null)
              {
                  PropertyChanged(this, new PropertyChangedEventArgs(propName));
              }
          }
      }
  </code></pre>
  
  <p>When started, a window with 2 textboxes and 1 textblock shows. Inside the window constructor, I have created an instance of person and assigned the DataContext of the stackPanel to this instance. The first textbox is binded to the FirstName property of the Person class, the second TextBox is binded to the LastName property and the final TextBlock just prints the LastName property followed by the FirstName properties. As I said earlier, I set the DataContext of the stackPanel inside the C# code. How can I set it instead inside the XAML? For example:</p>
  
  <pre><code>&lt;StackPanel x:Name="stackPanel" DataContext="person"&gt;
          &lt;TextBox x:Name="textBox_FirstName" Width="200" Margin="0,100,0,0" Text="{Binding Path=FirstName, UpdateSourceTrigger=PropertyChanged}"  /&gt;
          &lt;TextBox x:Name="textBox_LastName" Width="200" Margin="0,10,0,0" Text="{Binding Path=LastName, UpdateSourceTrigger=PropertyChanged}" /&gt;
          &lt;TextBlock x:Name="textBlock_FullName"  Background="LightBlue" Width="200" Margin="0,10,0,0" Text="{Binding Path=FullName, UpdateSourceTrigger=PropertyChanged}"  /&gt;
      &lt;/StackPanel&gt;
  </code></pre>
  
  <p>This doesn't work, but as you can see I am trying to set the DataContext of the stackPanel inside the XAML, how would I do it?</p>
  
  <p>Thanks!</p>