Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 156391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T10:19:15+00:00 2026-05-11T10:19:15+00:00

I have a problem when binding a command in a context menu on a

  • 0

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.

If I put a button that is bound to the command in the usercontrol it works as expected…

Can someone please tell me what I’m doing wrong??

This is a test project that exposes the problem:

App.xaml.cs:

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();     } } 

Window1.xaml:

<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'>    <Window.Resources>     <DataTemplate x:Key='HeaderTemplate'>         <StackPanel Orientation='Horizontal'>             <TextBlock Text='{Binding Path=Name}' />         </StackPanel>     </DataTemplate>     <DataTemplate DataType='{x:Type vw:PersonViewModel}'>         <vw:UserControl1/>     </DataTemplate>  </Window.Resources> <Grid>     <TabControl ItemsSource='{Binding Path=Persons}'                  ItemTemplate='{StaticResource HeaderTemplate}'                 IsSynchronizedWithCurrentItem='True' /> </Grid> </Window> 

UserControl1.xaml:

<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'>     <UserControl.ContextMenu>         <ContextMenu >             <MenuItem Header='Change' Command='{Binding Path=ChangeCommand}'/>         </ContextMenu>     </UserControl.ContextMenu>     <Grid>         <Grid.ColumnDefinitions>             <ColumnDefinition Width='100' />             <ColumnDefinition Width='*' />         </Grid.ColumnDefinitions>         <Label Grid.Column='0'>The name:</Label>         <TextBox Grid.Column='1' Text='{Binding Path=Name, UpdateSourceTrigger=PropertyChanged}' />     </Grid> </UserControl> 

CompanyViewModel.cs:

public class CompanyViewModel {     public ObservableCollection<PersonViewModel> Persons { get; set; }     public CompanyViewModel()     {         Persons = new ObservableCollection<PersonViewModel>();         Persons.Add(new PersonViewModel(new Person { Name = 'Kalle' }));         Persons.Add(new PersonViewModel(new Person { Name = 'Nisse' }));         Persons.Add(new PersonViewModel(new Person { Name = 'Jocke' }));     } } 

PersonViewModel.cs:

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; } 

TestCommand.cs:

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';     } } 

Person.cs:

public class Person {     public string Name { get; set; } } 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T10:19:16+00:00Added an answer on May 11, 2026 at 10:19 am

    The key thing to remember here is context menus are not part of the visual tree.

    Therefore they don’t inherit the same source as the control they belong to for binding. The way to deal with this is to bind to the placement target of the ContextMenu itself.

    <MenuItem Header='Change' Command='{Binding      Path=PlacementTarget.ChangeCommand,      RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}}' /> 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 186k
  • Answers 186k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The appscale project is designed to do exactly this. See… May 12, 2026 at 5:08 pm
  • Editorial Team
    Editorial Team added an answer The XMLHttpRequest object has a responseXML attribute which contains the… May 12, 2026 at 5:08 pm
  • Editorial Team
    Editorial Team added an answer Looks like the issue was with multiple projects having the… May 12, 2026 at 5:08 pm

Related Questions

I have a simple UserControl containing Label, ComboBox and Button. In brief, it is
This is similar to a question I asked a couple of days ago. However,
I have a problem with my WPF program. I'm trying to create an object
Having a bit of a hairy issue when submitting data over a jquery ajax

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.