开发者

WPF ContextMenu lose its subitems

开发者 https://www.devze.com 2023-01-12 12:20 出处:网络
I\'ve got a weird issue with WPF contextMenu regards updating the UI! Basically I create a hierarchical list named World.

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 contex开发者_如何学编程tMenu.

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


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号