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 1027281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T12:13:03+00:00 2026-05-16T12:13:03+00:00

I’ve got a weird issue with WPF contextMenu regards updating the UI! Basically I

  • 0

I’ve got a weird issue with WPF contextMenu regards updating the UI!

Basically I create a hierarchical list named World.
this list contain Countries and each country contain Cities.
I bind this list to a label contextMenu.

I create a button which delete one city in that list.

Here the code

`

<Window.Resources>
    <HierarchicalDataTemplate DataType="{x:Type local:Country}" ItemsSource="{Binding Path=ListCity}">
        <TextBlock Text="{Binding Path=NameCountry}"/>
    </HierarchicalDataTemplate>

    <HierarchicalDataTemplate DataType="{x:Type local:City}">
        <TextBlock Text="{Binding Path=NameCity}"/>
    </HierarchicalDataTemplate>
</Window.Resources>

<Grid>
    <Button x:Name="btnDelete_London" Click="btnDelete_London_Click" VerticalAlignment="Top">Del London</Button>

    <Label x:Name="label_World" Content="WORLD" VerticalAlignment="Bottom" HorizontalAlignment="Center" Background="Aqua"/>

</Grid>

`
code behind

 public class Country
{
    public string NameCountry { get; set; }
    public List<City> ListCity { get; set; }
}

public class City
{
    public string NameCity { get; set; }
}


public partial class MainWindow : Window
{        
    List<Country> World = new List<Country>();
    Country USA = new Country();
    Country UK = new Country();
    City NY = new City();
    City LA = new City();
    City LD = new City();

    public MainWindow()
    {
        InitializeComponent();

        USA.NameCountry = "United-Sates";
        UK.NameCountry = "United Kingdom";

        NY.NameCity = "New-York";            
        LA.NameCity = "Los Angeles";
        LD.NameCity = "London";

        USA.ListCity = new List<City>();
        USA.ListCity.Add(NY);
        USA.ListCity.Add(LA);

        UK.ListCity = new List<City>();
        UK.ListCity.Add(LD);

        World.Add(USA);
        World.Add(UK);

        this.label_World.ContextMenu= new ContextMenu();
        this.label_World.ContextMenu.ItemsSource = World;            
    }

    private void btnDelete_London_Click(object sender, RoutedEventArgs e)
    {
        bool finded = false;

        foreach (Country country in World)
        {
            foreach (City city in country.ListCity)
            {
                if (city.NameCity == "London")
                {    
                  country.ListCity.Remove(city);                    
                  finded = true;
                  break;
                }
            }
            if(finded ) break;
        }

        CollectionViewSource.GetDefaultView(label_World.ContextMenu.ItemsSource).Refresh();
    }
}

If at start you click on the button then you rightclick on the label, the contextMenu appear coorectly with updated data (i.e london deleted)

The probleme of UI updating appear when at start you first rightclick on the label that make the contextMenu display. Then you click somewhere in the window to make the contextMenu disappear. Then if you click in the button the city london get delete. Now when you rightclick on the label the contextMenu appear with Country Item but not with Their city Subitems

  • 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-05-16T12:13:04+00:00Added an answer on May 16, 2026 at 12:13 pm

    It looks like you are setting your ContextMenu ItemsSource to a copy of World, and not binding it to the instance of World that is created in your code behind.

    To set it as a binding, use this code in your constructor:

    this.label_World.ContextMenu = new ContextMenu();
    
    Binding worldBinding = new Binding();
    worldBinding.Source = World;
    this.label_World.ContextMenu.SetBinding(ContextMenu.ItemsSourceProperty, worldBinding);
    

    This will set the binding, however by default List<T> does not notify the UI when an object changes. You can make your life easier by replacing List<T> with System.Collections.ObjectModel.ObservableCollection<T> and then you can get rid of the line

    CollectionViewSource.GetDefaultView(label_World.ContextMenu.ItemsSource).Refresh(); 
    

    when you delete London because it will automatically update the context menu anytime the World object changes.

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Problem solved (to a point). I was running 64 bit… May 16, 2026 at 2:32 pm
  • Editorial Team
    Editorial Team added an answer Use the keyword "debugger;" to attempt invoking a hard breakpoint.… May 16, 2026 at 2:32 pm
  • Editorial Team
    Editorial Team added an answer Make sure you have #included the header file for QVector… May 16, 2026 at 2:32 pm

Trending Tags

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

Top Members

Related Questions

i got an object with contents of html markup in it, for example: string
I've got a string that has curly quotes in it. I'd like to replace
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I have a JSP page retrieving data and when single or double quotes are
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.