WPF ContextMenu 丢失其子项

发布于 2024-09-14 17:58:49 字数 2823 浏览 4 评论 0原文

我在 WPF contextMenu 更新 UI 方面遇到了一个奇怪的问题!

基本上我创建了一个名为 World 的分层列表。 该列表包含国家/地区,每个国家/地区包含城市。 我将此列表绑定到标签 contextMenu。

我创建了一个按钮来删除该列表中的一个城市。

这里的代码

`

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

` 代码隐藏

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

如果在开始时单击按钮,然后右键单击标签,上下文菜单会与更新的数据一起显示(即伦敦删除)

。 当开始时第一次右键单击使上下文菜单显示的标签时,会出现 UI 更新的问题。然后单击窗口中的某个位置以使 contextMenu 消失。然后,如果您单击按钮,伦敦市就会被删除。现在,当您右键单击标签时,上下文菜单会显示“国家/地区项目”,但不会显示“城市子项目”

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

何以心动 2024-09-21 17:58:49

看起来您正在将 ContextMenu ItemsSource 设置为 World 的副本,而不是将其绑定到在后面的代码中创建的 World 实例。

要将其设置为绑定,请在构造函数中使用以下代码:

this.label_World.ContextMenu = new ContextMenu();

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

这将设置绑定,但默认情况下,List 在对象更改时不会通知 UI。您可以通过将 List 替换为 System.Collections.ObjectModel.ObservableCollection 来让您的生活更轻松,然后

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

在删除 London 时 就可以摆脱该行因为只要世界对象发生变化,它就会自动更新上下文菜单。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文