呈现ItemsControl中项目的问题



我会保持简洁。我有一个实现ItemTemplate的ListBox。DataTemplate包含一个复选框。我装载了大约2000件物品。我选中前5项,滚动到底部并选择最后5项。然后我向上滚动到顶部项目,注意到我的前5个检查项目已经被修改。

    <Window 
        x:Class="CheckItems.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:CheckItems"
        Title="Window1" Height="300" Width="300"
        >
        <DockPanel>
            <StackPanel DockPanel.Dock="Bottom" >
                <Button Content="Test" Click="Button_Click"/>
            </StackPanel>
            <ListBox DockPanel.Dock="Left"
                x:Name="users"
                ItemsSource="{Binding Path=Users}"
                >
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <CheckBox>
                            <TextBlock Text="{Binding Path=Name}"/>
                        </CheckBox>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </DockPanel>
    </Window>

    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Windows;
    namespace CheckItems
    {
        public partial class Window1 : Window
        {
            ViewModel controller;
            public Window1()
            {
                DataContext = controller = new ViewModel();
                InitializeComponent();
                controller.Users = LoadData();
            }
            private List<User> LoadData()
            {
                var newList = new List<User>();
                for (var i = 0; i < 2000; ++i)
                    newList.Add(new User { Name = "Name" + i, Age = 100 + i });
                return newList;
            }
            private void Button_Click(object sender, RoutedEventArgs e)
            { }
        }
        public class User
        {
            public string Name { get; set; }
            public int Age { get; set; }
        }

        public class ViewModel : INotifyPropertyChanged
        {
            private List<User> users;
            public event PropertyChangedEventHandler PropertyChanged;
            public List<User> Users
            {
                get { return users; }
                set { users = value; NotifyChange("Users"); }
            }
            protected void NotifyChange(string propertyName)
            {
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

除此之外,希望还有一个很好的解释——这是一个MS错误。这种情况出现在.NET 3.5和4.0中。当VirtualingStackPanel.IsVirtualization设置为false时,这种行为不会发生,但在现实世界中,没有虚拟化的加载是痛苦的。

有一些见解会很好。

提前感谢

Andres Olivares

正在进行虚拟化的面板会重新使用其中的控件,并在滚动时简单地替换控件后面的DataContext。这意味着当您滚动时,控件的状态(如IsChecked)将被重置,除非该状态绑定到DataContext中的某个内容。

例如,如果2000中一次只有10个项目可见,那么WPF将只渲染其中的大约14个项目(滚动缓冲区的额外项目),并且在滚动和替换控件后面的DataContext时,只需重用这14个项目。

如果禁用虚拟化,则表示禁用了此回收行为。这意味着WPF将呈现2000个项目,而不是14个,这就是性能如此糟糕的原因。这也意味着复选框将保持选中状态,因为它们的状态不会重置。

要解决这个问题,我建议将IsSelected属性添加到User对象中,并将CheckBox.IsChecked绑定到它。

最新更新