2

Having an issue with a LisBox with a grouped CollectionViewSource ItemsSource in .NET 4.0. Pretty sure it's a bug, but can't find a good workaround. This is the entire code to repro the problem.

<Window x:Class="LBBug.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:LBBug"
    Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
    <local:ViewModel />
</Window.DataContext>
<Window.Resources>
    <CollectionViewSource Source="{Binding Data}" x:Key="cvs">
        <CollectionViewSource.GroupDescriptions>
            <PropertyGroupDescription PropertyName="GroupCol" />
        </CollectionViewSource.GroupDescriptions>
    </CollectionViewSource>
</Window.Resources>
<Grid>
    <ListBox ItemsSource="{Binding Source={StaticResource cvs}}" IsSynchronizedWithCurrentItem="True">
        <ListBox.GroupStyle>
            <GroupStyle>

            </GroupStyle>
        </ListBox.GroupStyle>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Path=Col1}" />
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Grid>
</Window>


public class Model
{
    public string GroupCol { get; set; }
    public string Col1 { get; set; }
    public string Col2 { get; set; }

}

public class ViewModel
{
    private List<Model> m_Data;
    public List<Model> Data
    {
        get
        {
            if (m_Data == null)
            {
                m_Data = new List<Model>();
                m_Data.Add(new Model { GroupCol = "Group1", Col1 = "Col1-1", Col2 = "Col2-1" });
                m_Data.Add(new Model { GroupCol = "Group1", Col1 = "Col1-1", Col2 = "Col2-1" });
                m_Data.Add(new Model { GroupCol = "Group1", Col1 = "Col1-1", Col2 = "Col2-1" });
                m_Data.Add(new Model { GroupCol = "Group2", Col1 = "Col1-2", Col2 = "Col2-2" });
                m_Data.Add(new Model { GroupCol = "Group2", Col1 = "Col1-2", Col2 = "Col2-2" });
                m_Data.Add(new Model { GroupCol = "Group2", Col1 = "Col1-2", Col2 = "Col2-2" });
            }
            return m_Data;
        }

    }

}

When Window launches, the first item is selected in the CVS and selected in ListBox:

[I can't post images, visualize a simple window with grouped listbox that has first item in the first group selected]

When I click on a new item (position 2 here), the initially selected item does not de-select. The CVS is updated with the newly selected item, but I can't re-select the first item (in position 0) by clicking directly on it once I move off of it.

[now visualize a listbox with first and 3rd items selected; yes, it's in single select mode]

If I hold the mouse down and drag up and down the list over the "stuck" item, it will de-select and start working. Also, if I use the keyboard to cursor back to the stuck item, it will de-select when I move down off of it with the keyboard. The problem is with the initial item selected in the CVS (i.e. not just position 0).

If I change the project to .NET 3.5, it works as expected. I've seen this reported, but thought I saw where it was fixed in 4.0.

Can you think of ANY reasonable work-around for this? I can sort of get it working by turning of IsSynchronized.. and manually watching changes to the CVS, then using a timer to set the current item on the LB, but that's getting really messy and hard to deal with all of the scenarios. I want to be able to manipulate the underlying data and have the ListBox keep up with what's selected.

Really hoping there's a straightforward way to deal with this. As it is, it appears you can't really do a grouped listbox in .NET 4; seems very odd it's not a more well-known issue.

4

0 回答 0