Reputation: 15886
I am currently having a ContextMenu on a ListView with its view style set to "GridView". However, this gives me trouble because you are able to right-click the visual columns in the top of the ListView to get the context-menu to appear as well.
I only want the context menu to appear on all the items in the list, and I would hate to program a method to add a new context-menu for every list or so.
Is there a smart way of doing this? Maybe through a template of some sort? Which approach would be the best one?
Upvotes: 3
Views: 3897
Reputation: 735
For reference, I couldn't get it to work like this, as it kept resulting in the error:
A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll
Additional information: Cannot add content of type 'System.Windows.Controls.ContextMenu' to an object of type 'System.Object'. Error at object 'System.Windows.Controls.ContextMenu' in markup file '<file>'.
Instead I had to use a static resource, which while having the exact same result, actually worked. Go figure.
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="ContextMenu" Value="{StaticResource ListViewContextMenu}" />
</Style>
</ListView.ItemContainerStyle>
<Application.Resources>
<ContextMenu x:Key="ListViewContextMenu">
<MenuItem Header="Play" />
</ContextMenu>
</Application.Resources>
Upvotes: 3
Reputation: 69242
You can create a style that will be applied to the items in the list view as opposed to the list view itself. Something like:
<ListView>
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu>
<MenuItem Header="Send Email" />
<MenuItem Header="Delete" />
</ContextMenu>
</Setter.Value>
</Setter>
</Style>
</ListView.ItemContainerStyle>
</ListView>
Upvotes: 8