Wrong choice in ListBox with VirtualizationMode = "Recycling" and SeclectionMode = "Extended"?

I have a really scary behavior. I have a ListBox in a view with a DataTemplate for its elements, including ViewModels. I bind IsSelected to my ViewModel and use SelectionMode = "Extended". Everything is working fine. BUT, if I add VirtualiuationMode = "Recycling", I get the wrong positions. To play: select the items with Ctrl, then scroll down and select only one item. Normal behavior deselects all the elements and simply selects the last one without holding Ctrl. But if I check my ViewModel, all old elements will be selected!?!

<Grid>
    <StackPanel>
        <ListBox ItemsSource="{Binding People}" MaxHeight="100"
                 SelectionMode="Extended"
                 VirtualizationMode="Recycling">
            <!--VirtualizingStackPanel.IsVirtualizing="True">-->

            <ListBox.ItemContainerStyle>
                <Style TargetType="{x:Type ListBoxItem}">
                    <Setter Property="IsSelected" Value="{Binding Path=IsSelected, Mode=TwoWay}" />

                </Style>

            </ListBox.ItemContainerStyle>
            <ListBox.ItemTemplate>
                <DataTemplate>

                    <views:PeopleView />

                </DataTemplate>

            </ListBox.ItemTemplate>
        </ListBox>
        <Button Click="Button_Click">
            OK
        </Button>
    </StackPanel>
</Grid>

Item template

<UserControl x:Class="WpfApplication1.View.PeopleView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="Auto" Width="Auto">
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" SharedSizeGroup="A"/>
        <ColumnDefinition Width="Auto" SharedSizeGroup="B"/>
    </Grid.ColumnDefinitions>
    <TextBox Text="{Binding Path=Name}" 
             Name="tbx_Name" 
           Grid.Column="0"/>

    <CheckBox IsChecked="{Binding Path=IstAktiv}"
        Name="cbx_IstAktiv" 
              Grid.Column="1"/>

</Grid>

Any idea?

+3
2

, "" , ?

    private void Lbx_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        ListBox lbx = (ListBox)sender;
        foreach (PersonViewModel item in lbx.Items)
        {
            item.IsSelected = lbx.SelectedItems.Contains(item);
        }
    }
+1

, KCT, AddedItems RemovedItems SelectionChangedEventArgs , :

private void Lbx_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    foreach (var item in e.AddedItems)
    {
        ((PersonViewModel)item).IsSelected = true;
    }
    foreach (var item in e.RemovedItems)
    {
        ((PersonViewModel)item).IsSelected = false;
    }
}

( 15 000 ListBox).

+1

Source: https://habr.com/ru/post/1747539/


All Articles