[英]Listview virtualization and deselecting all items
在我的应用程序中,我有一个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>
这是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;
}
我遇到了同样的问题并帮助自己从Sytem.Windows.Controls.ListBox
派生的自定义类重写OnSelectionChanged
,如此处所示VirtualizingStackPanel + MVVM +多选 。
不是我眼中的完美解决方案,而是工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.