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

  • SEARCH
  • Home
  • 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 7714357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:06:25+00:00 2026-06-01T02:06:25+00:00

I already have this issue but I cannot remember how to solved it. (I

  • 0

I already have this issue but I cannot remember how to solved it. (I think it’s related to the visual tree or the datacontext of the a contextMenu in wpf)

I have a ParentViewModel with a Combobox and a ContentPresenter.
The combobox display list of ChildViewModel. When one is selected it is displayed using the contentpresenter.

The ChildViewModel have a command to add items in a list. The command works find if it’s binded on a button, but when it’s done using a contextMenu the command is binded at the first execution but does not change if the ChildViewModel is changed (when another view model is selected in the combobox). The item is added to the previous selected ChildViewModel.

How can I solve this issue?

The Parent ViewModel:

public class Test1ViewModel:ObservableObject
{
    public Test1ViewModel()
    {
        ViewModels = new ObservableCollection<TestViewModel>();
        ViewModels.Add(new TestViewModel("View model1"));
        ViewModels.Add(new TestViewModel("View model2"));
        SelectedViewModel = ViewModels.FirstOrDefault();
    }

    private TestViewModel _selectedViewModel;
    public TestViewModel SelectedViewModel
    {
        get { return _selectedViewModel; }
        set
        {
            _selectedViewModel = value;
            RaisePropertyChanged(() => SelectedViewModel);
        }
    }

    public ObservableCollection<TestViewModel> ViewModels { get; set; }
}

The Parent View:

<StackPanel>
  <ComboBox ItemsSource="{Binding ViewModels}" DisplayMemberPath="Name" SelectedItem="{Binding SelectedViewModel}"></ComboBox>
  <ContentPresenter Content="{Binding SelectedViewModel}"/>
</StackPanel>

The Child ViewModel:

public class TestViewModel : ObservableObject
{
    private int _idx;
    public TestViewModel(string vmName)
    {
        Name = vmName;
        ListOfValues = new ObservableCollection<string>();
        ListOfValues.Add("Value" + _idx++);
        ListOfValues.Add("Value" + _idx++);
        AddItemCommand = new DelegateCommand(() => ListOfValues.Add("Value" + _idx++));
    }

    public string Name { get; private set; }

    public ObservableCollection<string> ListOfValues { get; set; }

    public DelegateCommand AddItemCommand { get; private set; }   
}

The Child View

<StackPanel>
<Button Content="AddValue" Command="{Binding AddItemCommand}"/> <!--Binding work when selected view model is changed-->
<TextBlock Text="{Binding Name}"/>
<ListBox ItemsSource="{Binding ListOfValues}">
  <ListBox.ContextMenu>
    <ContextMenu>
      <MenuItem Header="AddValue" Command="{Binding AddItemCommand}"/> <!--Binding doesn't work when selected view model is changed-->
    </ContextMenu>
  </ListBox.ContextMenu>
</ListBox>

Thanks in advance

  • 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. Editorial Team
    Editorial Team
    2026-06-01T02:06:27+00:00Added an answer on June 1, 2026 at 2:06 am

    You are right.

    The context menu is not in the visual tree, and only binds its datacontext once as the program is loaded.

    In order to fix it, what I do is implement Josh Smith’s Virtual Branch technique.

    Look at this answer I posted to a similar question

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am sorry I have already asked this question on Superuser, but nobody answers
I have already posted a question about this, but the situation has changed sufficiently
i have seen other blogs and SO questions relating to this issue but none
I have already have this code to force these URLs to HTTPS: RewriteCond %{HTTPS}
I have already googled for this I have a Table with following structure in
This is my php code (I already have a connection to the db): $result
This just saves time. Since I already have a web applciation. I can just
Possible Duplicate: What’s with the love of dynamic Languages I have already read this
Similar to this question: link However I have already mastered that. My problem is
im sorry if this was posted already i have been looking to no avail..

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.