【问题标题】:How to bind IsSelected in a ListView for non-visible (virtualized) items如何在 ListView 中为不可见(虚拟化)项目绑定 IsSelected
【发布时间】:2011-09-10 16:02:39
【问题描述】:

我需要在 WPF 应用程序中处理大量数据。

我已将大型集合绑定到 ListView,并且我正在使用 ItemContainerStyle 将列表项的 IsSelected 属性与我的对象的 IsSelected 属性绑定,这样当在 ListView 中选择该项目时,我的对象的 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>

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;
        }
    }
}

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

有人知道解决这个问题的方法吗?我无法关闭虚拟化,因为我会得到糟糕的性能。

【问题讨论】:

标签: wpf virtualization ui-virtualization


【解决方案1】:

我认为这是你的约束力。基本上你正在更新/绑定 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();
}

【讨论】:

  • 是的,玩了一会儿,我觉得最简单的方法是在代码中响应SelectionChanged,抱歉没有更好的给你..
  • 这就是我最终所做的。我只是想知道WPF中是否有更好的方法。看来不是。代码中的处理是唯一的方法。
猜你喜欢
  • 1970-01-01
  • 2020-08-26
  • 2023-03-15
  • 1970-01-01
  • 2016-07-05
  • 1970-01-01
  • 2016-09-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多