JAtkins

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.

Jay

Here is the XAML:

<Window x:Class="SDKSample.Window1"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

<Window.Resources>

<!--Define data for ListView-->

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

<x:XData>

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

</Info>

</x:XData>

</XmlDataProvider>

<ContextMenu x:Key="EmptyContextMenu">

<MenuItem Header="Test Item 1"/>

<MenuItem Header="Test Item 2"/>

</ContextMenu>

<!--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"/>

<Style.Triggers>

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

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

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

</Trigger>

<MultiTrigger>

<MultiTrigger.Conditions>

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

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

</MultiTrigger.Conditions>

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

</MultiTrigger>

</Style.Triggers>

</Style>

<!--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}}}"/>

</StackPanel>

</DataTemplate>

</Window.Resources>

<StackPanel>

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

Select Song to Play

</TextBlock>

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

XPath=Song}"

ItemContainerStyle="{StaticResource MyContainer}"

SelectionChanged="mySelectionChanged"

SelectionMode="Multiple"

Name="myPlaylist"

Margin="10,10,0,0">

<ListView.View>

<GridView>

<GridViewColumn CellTemplate="{StaticResource FirstCell}"

Width="30"/>

<GridViewColumn Header="Name"

DisplayMemberBinding="{Binding XPath=@Name}"

Width="80"/>

<GridViewColumn Header="Time"

DisplayMemberBinding="{Binding XPath=@Time}"

Width="80"/>

<GridViewColumn Header="Artist"

DisplayMemberBinding="{Binding XPath=@Artist}"

Width="80" />

<GridViewColumn Header="Level"

DisplayMemberBinding="{Binding XPath=@Level}"

Width="80"/>

<GridViewColumn Header="Disk"

DisplayMemberBinding="{Binding XPath=@Disk}"

Width="100"/>

</GridView>

</ListView.View>

</ListView>

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

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

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

</StackPanel>

</Window>

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 =

(XmlElement)myPlaylist.SelectedItem;

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

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

}

}

}



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

freeflyr

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.

Will





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

freeflyr

thanks, that works nicely.