2011-09-15 77 views
5

在我的应用程序中,我有一个ListView,它绑定到我的视图模型上的IsSelected属性。列表视图虚拟化和取消选择所有项目

我也在列表视图上设置了一个输入绑定,以便以编程方式处理选择列表中的所有项目,因为由于虚拟化堆栈面板,默认设置不起作用。这运作良好。

当用户按下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> 

这里是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; 
} 
+0

请发布您的SelectAllCommand – Paparazzi

回答

1

我遇到了同样的问题,并与Sytem.Windows.Controls.ListBox首要OnSelectionChanged派生的自定义类顺手拿如下所示VirtualizingStackPanel + MVVM + multiple selection

在我眼中不是一个完美的解决方案,但工作。

+0

我同意你的意见。试试这个解决方案:http://stackoverflow.com/a/29545790/2334614 – nvkokorin

相关问题