我已经测试了您的解决方案,它似乎是有序的:
MainWindow.xaml:
<Window x:Class="WpfApplication3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.Resources>
<Style TargetType='{x:Type ListViewItem}'>
<Setter Property="ContextMenu" Value="{StaticResource TreeContextMenuTest}" />
</Style>
</Grid.Resources>
<ListView>
<ListViewItem>aaa</ListViewItem>
<ListViewItem>bbb</ListViewItem>
<ListViewItem>ccc</ListViewItem>
</ListView>
</Grid>
应用程序.xaml:
<Application x:Class="WpfApplication3.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ContextMenu x:Key="TreeContextMenuTest" ItemsSource="{Binding ContextMenu}"/>
</Application.Resources>
应用程序.xaml.cs:
public partial class App : Application
{
public ObservableCollection<MenuItem> ContextMenu { get; set; }
public App()
{
ContextMenu = new ObservableCollection<MenuItem>();
var mi = new MenuItem {Name = "Test"};
ContextMenu.Add(mi);
}
}
我不知道您用来填充上下文菜单项的方式,我建议您不要在绑定中执行此操作,而是在 xaml 文件中执行此操作,但是会出现一个右键单击菜单项,如果我将其更改为标准的 menuItems 填充,他们也正确地出现了。
编辑:当说标准人口时,我的意思是这里解释:
http://wpftutorial.net/ContextMenu.html