Multiple Items Selected in SIngle Selection Grouped Listbox after VS11 / .Net 4.5 - by Berney

Status : 

  Duplicate<br /><br />
		This item appears to be a duplicate of another existing Connect or internal item.<br /><br />
		A more detailed explanation for the resolution of this particular item may have been provided in the comments section.


3
0
Sign in
to vote
ID 738020 Comments
Status Closed Workarounds
Type Bug Repros 2
Opened 4/20/2012 4:18:06 PM
Access Restriction Public
Moderator Decision Sent to Engineering Team for consideration

Description

I have a ListBox in my WPF app that is based on a grouped CollectionView binding. It has worked well for a year or two now. I installed VS 11 Beta which installed .Net Framework 4.5. I began seeing unusual behavior with the SelectedItem in the ListBox where multipliple items would becojke selected even though the SelectionMode is Single. This occurs most often after manually setting the SelectedIndex or SelectedItem. However, this is not the only case that causes the bug.  NOTE: I DO NOT HAVE DUPLICATE ITEMS IN THIS LIST. Setting the SelectedIndex to -1 before setting the SelectedIndex to a valid value did not help. This does not happen on machines with standard .Net Framework 4 installs, only on those with .Net Framework 4.5. After a miserable day long, error ridden process of uninstalling VS 11 & .Net 4.5 and SQL 2012 and repairing VS 2010 / .Net Framework and VS 2010 SP1, I can confirm that the app now behaves as normal. This is also confirmed on another test machine with .Net 4.5. Current I have 0 machines with 4.5 so I am not in a good position to test further.

Sign in to post a comment.
Posted by Microsoft on 5/1/2012 at 8:23 AM
That works fine in the latest 4.5 bits.
- Sam
Posted by Licantrop0 on 5/1/2012 at 7:44 AM
Sure, here is the XAML:

<Window x:Class="Listbox_Bug.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded_1">
    
    <Window.Resources>
        <CollectionViewSource x:Key="cvs">
            <CollectionViewSource.GroupDescriptions>
                <PropertyGroupDescription PropertyName="Key"/>
            </CollectionViewSource.GroupDescriptions>
        </CollectionViewSource>
    </Window.Resources>
    <Grid>
        <ListBox ItemsSource="{Binding Source={StaticResource cvs}}">
            <ListBox.GroupStyle>
                <GroupStyle>
                    <GroupStyle.HeaderTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Name}" Foreground="Red" />
                        </DataTemplate>
                    </GroupStyle.HeaderTemplate>
                </GroupStyle>
            </ListBox.GroupStyle>
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Value}" />
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</Window>

and here is the code:

namespace Listbox_Bug
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded_1(object sender, RoutedEventArgs e)
        {
            var cvs = this.FindResource("cvs") as CollectionViewSource;
            cvs.Source = GetData();
        }

        private IEnumerable<KeyValuePair<string, string>> GetData()
        {
            return new[]
            {
                new KeyValuePair<string, string>("Beverages", "Coca Cola"),
                new KeyValuePair<string, string>("Beverages", "Sprite"),
                new KeyValuePair<string, string>("Beverages", "Fanta"),
                new KeyValuePair<string, string>("Food", "Hamburgher"),
                new KeyValuePair<string, string>("Food", "Tacos"),
                new KeyValuePair<string, string>("Food", "Pasta"),
                new KeyValuePair<string, string>("Food", "Pizza"),
            };
        }
    }
}
Posted by Microsoft on 4/27/2012 at 11:07 AM
It can't be Josh Smith's bug - we fixed that in WPF 4.0.

From your description, it sounds very much like a bug we discovered and fixed since releasing Beta. The fix is in the final release. I can't verify this with only snippets of an app - can you attach a complete app that demonstrates the problem?

- Sam (WPF team)
Posted by Licantrop0 on 4/27/2012 at 4:25 AM
It seems an old bug, similar to this: http://joshsmithonwpf.wordpress.com/2008/09/18/the-initially-selected-item-when-binding-to-a-grouped-icollectionview/

I can easily Repro the bug with a grouped ListBox in binding with a CollectionViewSource with a PropertyGroupDescription setted.
Posted by MS-Moderator08 [Feedback Moderator] on 4/23/2012 at 1:43 AM
Thank you for submitting feedback on Visual Studio 11 and .NET Framework. Your issue has been routed to the appropriate VS development team for review. We will contact you if we require any additional information.