title: WPF Context menu doesn't bind to right databound item
body: <p>I have a problem when binding a command in a context menu on a usercontrol that is on a tab page.
  The first time I use the menu (right-click on the tab) it works great, but if I switch tab the command will use the databound instance that was used the first time.</p>
  
  <p>If I put a button that is bound to the command in the usercontrol it works as expected...</p>
  
  <p>Can someone please tell me what I'm doing wrong??</p>
  
  <p>This is a test project that exposes the problem:</p>
  
  <p>App.xaml.cs:</p>
  
  <pre><code>public partial class App : Application
  {
      protected override void OnStartup(StartupEventArgs e)
      {
          base.OnStartup(e);
  
          CompanyViewModel model = new CompanyViewModel();
          Window1 window = new Window1();
          window.DataContext = model;
          window.Show();
      }
  }
  </code></pre>
  
  <p>Window1.xaml:</p>
  
  <pre><code>&lt;Window x:Class="WpfApplication1.Window1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:vw="clr-namespace:WpfApplication1"
  Title="Window1" Height="300" Width="300"&gt;
  
    &lt;Window.Resources&gt;
      &lt;DataTemplate x:Key="HeaderTemplate"&gt;
          &lt;StackPanel Orientation="Horizontal"&gt;
              &lt;TextBlock Text="{Binding Path=Name}" /&gt;
          &lt;/StackPanel&gt;
      &lt;/DataTemplate&gt;
      &lt;DataTemplate DataType="{x:Type vw:PersonViewModel}"&gt;
          &lt;vw:UserControl1/&gt;
      &lt;/DataTemplate&gt;
  
  &lt;/Window.Resources&gt;
  &lt;Grid&gt;
      &lt;TabControl ItemsSource="{Binding Path=Persons}" 
                  ItemTemplate="{StaticResource HeaderTemplate}"
                  IsSynchronizedWithCurrentItem="True" /&gt;
  &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>UserControl1.xaml:</p>
  
  <pre><code>&lt;UserControl x:Class="WpfApplication1.UserControl1"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      MinWidth="200"&gt;
      &lt;UserControl.ContextMenu&gt;
          &lt;ContextMenu &gt;
              &lt;MenuItem Header="Change" Command="{Binding Path=ChangeCommand}"/&gt;
          &lt;/ContextMenu&gt;
      &lt;/UserControl.ContextMenu&gt;
      &lt;Grid&gt;
          &lt;Grid.ColumnDefinitions&gt;
              &lt;ColumnDefinition Width="100" /&gt;
              &lt;ColumnDefinition Width="*" /&gt;
          &lt;/Grid.ColumnDefinitions&gt;
          &lt;Label Grid.Column="0"&gt;The name:&lt;/Label&gt;
          &lt;TextBox Grid.Column="1" Text="{Binding Path=Name, UpdateSourceTrigger=PropertyChanged}" /&gt;
      &lt;/Grid&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <p>CompanyViewModel.cs:</p>
  
  <pre><code>public class CompanyViewModel
  {
      public ObservableCollection&lt;PersonViewModel&gt; Persons { get; set; }
      public CompanyViewModel()
      {
          Persons = new ObservableCollection&lt;PersonViewModel&gt;();
          Persons.Add(new PersonViewModel(new Person { Name = "Kalle" }));
          Persons.Add(new PersonViewModel(new Person { Name = "Nisse" }));
          Persons.Add(new PersonViewModel(new Person { Name = "Jocke" }));
      }
  }
  </code></pre>
  
  <p>PersonViewModel.cs:</p>
  
  <pre><code>public class PersonViewModel : INotifyPropertyChanged
  {
      Person _person;
      TestCommand _testCommand;
  
      public PersonViewModel(Person person)
      {
          _person = person;
          _testCommand = new TestCommand(this);
      }
      public ICommand ChangeCommand 
      {
          get
          {
              return _testCommand;
          }
      }
      public string Name 
      {
          get
          {
              return _person.Name;
          }
          set
          {
              if (value == _person.Name)
                  return;
              _person.Name = value;
              OnPropertyChanged("Name");
          }
      }
      void OnPropertyChanged(string propertyName)
      {
          PropertyChangedEventHandler handler = this.PropertyChanged;
          if (handler != null)
          {
              var e = new PropertyChangedEventArgs(propertyName);
              handler(this, e);
          }
      }
      public event PropertyChangedEventHandler PropertyChanged;
  }
  </code></pre>
  
  <p>TestCommand.cs:</p>
  
  <pre><code>public class TestCommand : ICommand
  {
      PersonViewModel _person;
      public event EventHandler CanExecuteChanged;
  
      public TestCommand(PersonViewModel person)
      {
          _person = person;
      }
      public bool CanExecute(object parameter)
      {
          return true;
      }
      public void Execute(object parameter)
      {
          _person.Name = "Changed by command";
      }
  }
  </code></pre>
  
  <p>Person.cs:</p>
  
  <pre><code>public class Person
  {
      public string Name { get; set; }
  }
  </code></pre>