列表视图虚拟化并取消选择所有项目

Bor*_*ort 7 c# wpf xaml listview

在我的应用程序中,我有一个listview,其项目容器样式绑定到我的视图模型上的IsSelected属性.

我还在listview上设置了一个inputbinding来处理以编程方式选择列表中的所有项目,因为默认情况下由于虚拟化堆栈面板不起作用.这很好用.

当用户按下CTRL + A后单击单个列表项时出现问题.用户应该期望发生的是单击的单个新项目成为唯一选择的项目.实际发生的是listview不会更新看不到的项目的IsSelected属性,只有当前可见的项目才会被取消选中.

我该如何正确处理这种行为?

<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>
Run Code Online (Sandbox Code Playgroud)

这是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;
}
Run Code Online (Sandbox Code Playgroud)