如何在ListView中为不可见(虚拟化)项绑定IsSelected

Ove*_*Ove 9 wpf virtualization ui-virtualization

我需要处理WPF应用程序中的大量数据.

我已将大型集合绑定到ListView,并且我使用ItemContainerStyle将列表项的IsSelected属性绑定到我的对象的IsSelected属性,以便在选中该项时ListView,my objects的IsSelected属性也将设置为true.通过这样做,我可以轻松地仅对列表中已选择的对象执行命令.

我在ListView中使用UI虚拟化,因为应用程序会缓慢.但是因为我的整个集合中只有一部分在列表中可见,所以当我使用CTRL + A选择列表中的所有项目时,只有加载的项目的IsSelected属性设置为true.不可见的项目(虚拟化的项目)的IsSelected属性设置为false.这是一个问题,因为当我选择列表中的所有项目时,我希望该IsSelected属性对于集合中的所有项目都设置为true.

我已经创建了一些示例代码来说明问题:

MainWindow.xaml

<Window x:Class="VirtualizationHelp.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525" x:Name="wnd">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <Button Grid.Row="0" Content="Click me" Click="Button_Click" />
    <ListView Grid.Row="1" ItemsSource="{Binding Path=Persons, ElementName=wnd}">
        <ListView.ItemContainerStyle>
            <Style TargetType="{x:Type ListViewItem}">
                <Setter Property="IsSelected" Value="{Binding Path=IsSelected, Mode=TwoWay}"/>
            </Style>
        </ListView.ItemContainerStyle>
    </ListView>
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)

MainWindow.xaml.cs

using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace VirtualizationHelp
{
    public partial class MainWindow : Window
    {
        List<SelectablePerson> _persons = new List<SelectablePerson>(10000);
        public List<SelectablePerson> Persons { get { return _persons; } }
        public MainWindow()
        {
            for (int i = 0; i < 10000; i++)
            {
                SelectablePerson p = new SelectablePerson() { Name = "Person " + i, IsSelected = false };
                _persons.Add(p);
            }
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            int count = Persons.Where(p => p.IsSelected == true).Count();
            (sender as Button).Content = count.ToString();
        }
    }

    public class SelectablePerson
    {
        public string Name { get; set; }
        public bool IsSelected { get; set; }
        public override string ToString()
        {
            return Name;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

单击表单顶部的按钮时,它会计算集合中"IsSelected"属性设置为true的项目.您可以看到,当您按CTRL+ A选择列表中的所有项目时,它会显示只选择了19个项目.

有没有人知道解决这个问题的方法?我无法关闭虚拟化,因为我会得到可怕的性能.

den*_*zov 3

我认为这是你的束缚。基本上,您正在更新/绑定 ListViewItem,这只会更新可见的项目,而不是整个列表。我会玩它,现在你可以在代码绑定中解析..

<Window x:Class="VirtualizationHelp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" x:Name="wnd">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <TextBlock x:Name="txtSelectedItemsCount"/>
        <ListView Grid.Row="1" ItemsSource="{Binding Path=Persons, ElementName=wnd}" 
         SelectionChanged="ListView_SelectionChanged"/>
    </Grid>
</Window>

private void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    var lv = (ListView) sender;
    txtSelectedItemsCount.Text = lv.SelectedItems.Count.ToString();
}
Run Code Online (Sandbox Code Playgroud)