ListView virtualization and deletion of all items

In my application, I have a listview with the element container type bound to the IsSelected property in my view model.

I also created an input binding in the list to handle the selection of all the items in the list programmatically, since it does not work by default because of the virtualization stack panel. It works well and well.

The problem occurs when the user clicks on one list item after pressing CTRL + A. What the user should expect is the only selected item, which will become the only selected item. In fact, listview does not update the IsSelected property for items that are out of view, and only the items currently visible become inaccessible.

How can I handle this behavior correctly?

<ListView
    Name="sortList"
    Grid.Row="1" 
    ItemsSource="{Binding RelativeSource={RelativeSource 
    FindAncestor, AncestorType={x:Type UserControl}}, 
    Path=ItemsSource, Mode=TwoWay}">
    <ListView.InputBindings>
        <KeyBinding Gesture="CTRL+A" Command="{Binding SelectAllCommand}" />
    </ListView.InputBindings>

    <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
            <Setter Property="Padding" Value="3" />
            <Setter 
                Property="IsSelected" 
                Value="{Binding Path=IsSelected, Mode=TwoWay}" />
        </Style>
    </ListView.ItemContainerStyle>
</ListView>

select all.

private RelayCommand _selectAllCommand;
public System.Windows.Input.ICommand SelectAllCommand
{
    get
    {
        if (_selectAllCommand == null)
            _selectAllCommand = new RelayCommand(param => this.SelectAll());
        return _selectAllCommand;
    }
}

private void SelectAll()
{
    foreach (object o in this.Objects)
       if (!this.SelectedItems.Contains(order))
           order.IsSelected = true;
}
+5
1

, Sytem.Windows.Controls.ListBox overriding OnSelectionChanged, VirtualizingStackPanel + MVVM + multiple selection.

, .

+1

All Articles