4

I have a ListView set up as follows:

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

Yet when I right-click an item in the listview, and I expect the context menu to pop up, nothing happens. All examples I have seen are as simple as my code, but what am I doing wrong that the menu isn't appearing?

ProfK
  • 49,207
  • 121
  • 399
  • 775
  • Is the control active before you click right mouse button? – Alex Seleznyov Mar 05 '18 at 13:16
  • If you're expecting the context menu when you click on an item then wouldn't the context menu have to be defined against the ListViewItem? I'm wondering if this approach would work: https://stackoverflow.com/questions/747872/wpf-displaying-a-context-menu-for-a-gridviews-items – LordWilmore Mar 05 '18 at 13:19
  • That's strange it's working on my end? What is type of UIElement is your ListView a child of? – Tronald Mar 05 '18 at 13:42
  • Also, does right click at least give focus to the item that is clicked? – Tronald Mar 05 '18 at 13:43
  • @LordWilmore As I said, I'm expecting the context-menu when I *right*-click an item. Isn't that the normal behaviour for a context menu on a list or grid view? – ProfK Mar 06 '18 at 05:38
  • @Tronald The ListView is a child of a grid cell in the MainWindow. And, yes, it does give focus to the item that is clicked. – ProfK Mar 06 '18 at 05:43
  • @AlexSeleznyov If I right-click on the ListView it is inevitable that the ListView gets focus and is active. – ProfK Mar 06 '18 at 05:45
  • @ProfK yes sure, I got it that it was a right-click, but my point is that you're defining the context menu against the ListView, and not the ListViewItem. The link that I provided had a suitable chunk of xaml that should give you what you want. – LordWilmore Mar 06 '18 at 09:03
  • @LordWilmore Thanks, after some careful revision I have a context menu. Why don't you answer and I'll accept. – ProfK Mar 06 '18 at 13:12
  • Ok, thanks I'll do that now – LordWilmore Mar 06 '18 at 13:28

1 Answers1

2

I think the likely answer is that you are creating the context menu against the ListView and it should be created against the ListViewItem. I believe that you are never actually clicking on the ListView itself, but rather always on the items, so therefore the context menu is never being invoked.

Suggest you need to structure your ListView xaml something like this:

<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>
LordWilmore
  • 2,829
  • 2
  • 25
  • 30
  • 1
    I declared the context menu in `ListView.Resources` and used a static resource inside the style, like ``, but your answer steered me in the right direction to come right. – ProfK Mar 07 '18 at 10:08