---
title: "ContextMenu for ListViewItem only"  
description: "ContextMenu for ListViewItem only"  
author: "marcel ethan"  
published: 2013-09-23  
updated: 2013-09-23  
canonical: https://www.mindstick.com/forum/1491/contextmenu-for-listviewitem-only  
category: "wpf"  
tags: ["wpf", "mindstick", "ListViewItem", "ContextMenu", "ContextMenu for ListViewItem"]  
reading_time: 1 minute  

---

# ContextMenu for ListViewItem only

I have a [context](https://www.mindstick.com/forum/23245/what-is-context-in-android) menu - [problem](https://yourviews.mindstick.com/view/81399/tackling-the-problem-of-unemployment-during-corona-pandemic) is I need it to only open when a listviewitem is clicked. [Right now](https://answers.mindstick.com/qa/36863/what-would-happen-if-gravity-became-5-percent-stronger-right-now) it will open if I click anywhere in the [listview](https://www.mindstick.com/articles/12744/listview-in-android) or in the header.

<ListView>

<ListView.ContextMenu>

<ContextMenu>

<[MenuItem](https://www.mindstick.com/forum/23048/menuitem-call-javascript-function) Header="More Info" [Command](https://www.mindstick.com/blog/178/synchronous-and-asynchronous-command-execution-in-c-sharp-dot-net)="{Binding MoreInfo}" />

</ContextMenu>

</ListView.ContextMenu>

<ListView.View>

<[GridView](https://www.mindstick.com/articles/873/update-delete-edit-gridview-in-asp-dot-net)>

<!-- columns and stuff here -->

</GridView>

</ListView.View>

</ListView>

I have tried adding the ContextMenu as a [resource](https://www.mindstick.com/blog/43433/top-best-resources-for-women-about-beauty-and-health) and applying it as a style, but this breaks the command (clicking on More Info should open a dialog window, doesnt work this way)

<ListView.Resources>

<ContextMenu x:Key="ItemContextMenu">

<MenuItem Header="More Info" Command="{Binding MoreInfo}" [Background](https://www.mindstick.com/articles/65/how-to-change-background-color-of-tab-control-in-c-sharp)="WhiteSmoke" />

</ContextMenu>

</ListView.Resources>

<ListView.ItemContainerStyle>

<Style TargetType="{x:Type ListViewItem}" >

<Setter [Property](https://www.mindstick.com/blog/205/property-notification-in-c-sharp)="ContextMenu" Value="{StaticResource ItemContextMenu}" />

</Style>

</ListView.ItemContainerStyle>

So not sure how to restrict the context menu to only the listviewitem and have the command work.

## Replies

### Reply by Sumit Kesarwani

Hi Marcel,

Use the RelativeSource in the command binding in the template, and it will work:

```
<ListView.Resources>    <ContextMenu
x:Key="ItemContextMenu">        <MenuItem
Header="More Info" Command="{Binding Path=DataContext.MoreInfo,
RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListView}}"
Background="WhiteSmoke" />   
</ContextMenu></ListView.Resources><ListView.ItemContainerStyle>    <Style
TargetType="{x:Type ListViewItem}" >        <Setter
Property="ContextMenu" Value="{StaticResource ItemContextMenu}"
/>    </Style></ListView.ItemContainerStyle>
```


---

Original Source: https://www.mindstick.com/forum/1491/contextmenu-for-listviewitem-only

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
