title: How to use command bindings in user controls in wpf?
body: <p>In MainWindow the commandbinding works fine.
  In UserControl1 it doesnt work.  Note the datacontext is set correctly as is evidenced by the content of the button which is the result of a binding.</p>
  
  <p>I am not trying to bind the command in the usercontrol to a command in mainwindow or any other such trickery.  I am just trying to replicate what I did in MainWindow in UserControl1.</p>
  
  <p><strong>MainWindow XAML</strong></p>
  
  <pre><code>&lt;StackPanel&gt;
      &lt;Button Content="Click Here" Command="{Binding ClickHereCommand}" Height="25" Width="90"&gt;&lt;/Button&gt;
      &lt;local:UserControl1&gt;&lt;/local:UserControl1&gt;
  &lt;/StackPanel&gt;
  </code></pre>
  
  <p><strong>MainWindow Code Behind</strong></p>
  
  <pre><code>public partial class MainWindow : Window
  {
      public static RoutedCommand ClickHereCommand { get; set; }
  
      public MainWindow()
      {
          InitializeComponent();
          this.DataContext = this;
          ClickHereCommand = new RoutedCommand();
          CommandBindings.Add(new CommandBinding(ClickHereCommand, ClickHereExecuted));
      }
  
      public void ClickHereExecuted(object sender, ExecutedRoutedEventArgs e)
      {
          System.Windows.MessageBox.Show("hello");
      }
  }
  </code></pre>
  
  <p><strong>UserControl XAML</strong></p>
  
  <pre><code>&lt;UserControl x:Class="CommandBindingTest.UserControl1"
           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
           xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
           xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
           mc:Ignorable="d" 
           d:DesignHeight="300" d:DesignWidth="300" x:Name="root"&gt;
  
  &lt;Grid DataContext="{Binding ElementName=root}" &gt;
      &lt;Button Content="{Binding ButtonContent}" Command="{Binding ClickHereCommand}" Height="25" Width="90"&gt;&lt;/Button&gt;
  &lt;/Grid&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <p><strong>UserControl Code Behind</strong></p>
  
  <pre><code>public partial class UserControl1 : UserControl, INotifyPropertyChanged
  {
      private string _ButtonContent;
      public string ButtonContent
      {
          get { return _ButtonContent; }
          set
          {
              if (_ButtonContent != value)
              {
                  _ButtonContent = value;
                  OnPropertyChanged("ButtonContent");
              }
          }
      }
  
      public static RoutedCommand ClickHereCommand { get; set; }
  
  
      public UserControl1()
      {
          InitializeComponent();
          ClickHereCommand = new RoutedCommand();
          CommandBindings.Add(new CommandBinding(ClickHereCommand, ClickHereExecuted));
          ButtonContent = "Click Here";
      }
  
      public void ClickHereExecuted(object sender, ExecutedRoutedEventArgs e)
      {
          System.Windows.MessageBox.Show("hello from UserControl1");
      }
  
  
      #region INotifyPropertyChanged Members
      public event PropertyChangedEventHandler PropertyChanged;
  
      public void OnPropertyChanged(string name)
      {
          if (PropertyChanged != null)
          {
              PropertyChanged(this, new PropertyChangedEventArgs(name));
          }
      }
      #endregion
  }
  </code></pre>