Why is my WPF Context Menu not working on a ListView?
我有一个
1 2 3 4 5 6 7 8 9 10 11 12 13 | <ListView x:Name="LocalGrid" DataContext="{Binding LocalFiles}" ItemsSource="{Binding Items}" SelectionMode="Single" HorizontalAlignment="Stretch"> <ListView.ContextMenu> <ContextMenu> <MenuItem Header="Download" Click="ServerFilesDownloadOnClick"></MenuItem> </ContextMenu> </ListView.ContextMenu> <ListView.View> <GridView AllowsColumnReorder="True"> <GridViewColumn DisplayMemberBinding="{Binding Name}" Header="Name" Width="Auto" /> <GridViewColumn DisplayMemberBinding="{Binding Length}" Header="Size" Width="100" /> </GridView> </ListView.View> </ListView> |
然而,当我右键单击列表视图中的一个项目时,我希望上下文菜单会弹出,但没有任何反应。我见过的所有例子都和我的代码一样简单,但是我做错了什么菜单没有出现?
我认为可能的答案是您正在针对
建议您需要像这样构建您的 ListView xaml:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <ListView x:Name="LocalGrid" ...> <ListView.ItemContainerStyle> <Style TargetType="{x:Type ListViewItem}"> <Setter Property="ContextMenu"> <Setter.Value> <ContextMenu> <MenuItem Header="Download" Click="ServerFilesDownloadOnClick" /> </ContextMenu> </Setter.Value> </Setter> </Style> </ListView.ItemContainerStyle> <ListView.View> ... </ListView.View> </ListView> |