forum

Home / DeveloperSection / Forums / ContextMenu for ListViewItem only

ContextMenu for ListViewItem only

marcel ethan 4643 23-Sep-2013

I have a context menu - problem is I need it to only open when a listviewitem is clicked. Right now it will open if I click anywhere in the listview or in the header.

<ListView>

    <ListView.ContextMenu>

        <ContextMenu>

            <MenuItem Header="More Info" Command="{Binding MoreInfo}" />

        </ContextMenu>

     </ListView.ContextMenu>

     <ListView.View>

         <GridView>

           <!-- columns and stuff here -->

         </GridView>          

     </ListView.View>

 </ListView>

I have tried adding the ContextMenu as a resource 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="WhiteSmoke" />

    </ContextMenu>

</ListView.Resources>

<ListView.ItemContainerStyle>

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

        <Setter Property="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.


wpf
Updated on 23-Sep-2013

Can you answer this question?


Answer

1 Answers

Liked By