I hope this isnt a dumb question, but I'm hoping someone can help me understand the expected behavior of right-click in a ListView. Here is the problem:

If you have a context menu on a ListView in Multiple or Extended selection mode a right click to bring up the context menu will toggle the selection. I have mocked up a small repro using the ListViewCheckBox example in the SDK. I've inlcuded the modified XAML and the unchanged codebehind. All I did was add a contextmenu to the listviewitem.

How to repro:

1. Select Multiple items

2. Right click on a selected item to bring up the context menu. This action will toggle the selection to unselected. The item is no longer viewable in your collection.

Based on my observations the WPF control does not have the same behavior as the legacy .NET 2.0 Forms control. In the legacy control the selection is not affected by a right click.

I'm hoping this isnt the expected behavior, but if it is I am looking to understand what is the best way to deal with it. Right now we are programmatically reselecting the item, but that causes a visible deselect/reselect on the screen. The only way to not affect selection in the ListView has been to use the Shift-F10 context menu shortcut.

Thanks in advance.


Here is the XAML:

<Window x:Class="SDKSample.Window1"




<!--Define data for ListView-->

<XmlDataProvider x:Key="MyData" XPath="/Info">


<Info xmlns="">

<Song Name="Song 1" Time="3:54" Artist="Artist 1" Level="3"

Disk="Disk 2"/>

<Song Name="Song 2" Time="4:31" Artist="Artist 2" Level="4"

Disk="Disk 1" />

<Song Name="Song 3" Time="4:31" Artist="Artist 1" Level="2"

Disk="Disk 1" />




<ContextMenu x:Key="EmptyContextMenu">

<MenuItem Header="Test Item 1"/>

<MenuItem Header="Test Item 2"/>


<!--Define the style for each row of song data including

triggers that change the color scheme of a cell

when it is selected or the mouse passes over it-->

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

<Setter Property="ContextMenu" Value="{StaticResource EmptyContextMenu}"/>

<Setter Property="Margin" Value="0,1,0,0"/>

<Setter Property="Height" Value="21"/>


<Trigger Property="IsMouseOver" Value="true">

<Setter Property="Foreground" Value="Blue" />

<Setter Property="Cursor" Value="Hand"/>




<Condition Property="IsSelected" Value="true" />

<Condition Property="Selector.IsSelectionActive" Value="true" />


<Setter Property="Foreground" Value="Yellow" />




<!--Define the template for the first column to contain

a CheckBox control-->

<DataTemplate x:Key="FirstCell">

<StackPanel Orientation="Horizontal">

<CheckBox IsChecked="{Binding Path=IsSelected,

RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListViewItem}}}"/>





<TextBlock Foreground="Blue" FontSize="14" Margin="10,10,0,0">

Select Song to Play


<ListView ItemsSource="{Binding Source={StaticResource MyData},


ItemContainerStyle="{StaticResource MyContainer}"







<GridViewColumn CellTemplate="{StaticResource FirstCell}"


<GridViewColumn Header="Name"

DisplayMemberBinding="{Binding XPath=@Name}"


<GridViewColumn Header="Time"

DisplayMemberBinding="{Binding XPath=@Time}"


<GridViewColumn Header="Artist"

DisplayMemberBinding="{Binding XPath=@Artist}"

Width="80" />

<GridViewColumn Header="Level"

DisplayMemberBinding="{Binding XPath=@Level}"


<GridViewColumn Header="Disk"

DisplayMemberBinding="{Binding XPath=@Disk}"





<TextBlock Margin="10,10,0,0">Now Playing:</TextBlock>

<TextBlock Margin="20,10,0,0" Name="NowPlaying"

FontSize="12" Foreground="Purple"></TextBlock>



Code Behind:

using System;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Data;

using System.Windows.Documents;

using System.Windows.Media;

using System.Windows.Shapes;

using System.Windows.Controls.Primitives;

using System.Collections.ObjectModel;

using System.Xml;

namespace SDKSample


/// <summary>

/// Interaction logic for Window1.xaml

/// </summary>

public partial class Window1 : Window


void mySelectionChanged(object sender,

SelectionChangedEventArgs e)


XmlElement mySelectedElement =


NowPlaying.Text = mySelectedElement.GetAttribute("Name").ToString()

+ " by " +mySelectedElement.GetAttribute("Artist").ToString();




Re: Windows Presentation Foundation (WPF) ListViewItem w/Context Menu


I'm seeing exactly the same behaviour ... anyone from MSFT maybe have a work around for this Is there some other property that needs to be set

Thanks in advance.


Re: Windows Presentation Foundation (WPF) ListViewItem w/Context Menu

lee d

you can handle the PreviewMouseRightButtonDown of listview and set e.Handled=true

Re: Windows Presentation Foundation (WPF) ListViewItem w/Context Menu


thanks, that works nicely.