WPF ItemsControl load huge 50M+ data

<Window x:Class="WpfApp2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp2"
        mc:Ignorable="d"
        Title="{Binding MainTitle}" WindowState="Maximized">
    <Window.DataContext>
        <local:MainVM/>
    </Window.DataContext>
    <Grid>
        <ItemsControl ItemsSource="{Binding BooksCollection}">
            <ItemsControl.Resources>
                <Style TargetType="MenuItem">
                    <Setter Property="FontSize" Value="30"/>
                    <Style.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="FontSize" Value="50"/>
                            <Setter Property="Foreground" Value="Red"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </ItemsControl.Resources>
            <ItemsControl.Template>
                <ControlTemplate>
                    <ScrollViewer CanContentScroll="True">
                        <VirtualizingStackPanel IsItemsHost="True"
                                                VirtualizingPanel.IsVirtualizing="True"
                                                VirtualizingPanel.VirtualizationMode="Recycling"
                                                VirtualizingPanel.CacheLengthUnit="Item"
                                                VirtualizingPanel.CacheLength="2,2"
                                                ScrollViewer.CanContentScroll="True"
                                                ScrollViewer.IsDeferredScrollingEnabled="True"
                                                UseLayoutRounding="True"
                                                SnapsToDevicePixels="True">                            
                        </VirtualizingStackPanel>
                    </ScrollViewer>
                </ControlTemplate>
            </ItemsControl.Template>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Grid Margin="20">
                        <Grid.Resources>
                            <Style TargetType="TextBlock">
                                <Setter Property="FontSize" Value="30"/>
                                <Style.Triggers>
                                    <Trigger Property="IsMouseOver" Value="True">
                                        <Setter Property="FontSize" Value="40"/>
                                        <Setter Property="Foreground" Value="Red"/>
                                    </Trigger>
                                </Style.Triggers>
                            </Style>
                        </Grid.Resources>
                        <Grid.RowDefinitions>
                            <RowDefinition/>
                            <RowDefinition/>
                            <RowDefinition/>
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition/>
                            <ColumnDefinition/>
                            <ColumnDefinition/>
                        </Grid.ColumnDefinitions>
                        <TextBlock Text="{Binding Id}" Grid.Row="0" Grid.Column="0"/>
                        <TextBlock Text="{Binding Name}" Grid.Row="0" Grid.Column="1"/>
                        <TextBlock Text="{Binding ISBN}" Grid.Row="0" Grid.Column="2"/>
                        <TextBlock Text="{Binding Author}" Grid.Row="1" Grid.Column="0"/>
                        <TextBlock Text="{Binding Abstract}" Grid.Row="1" Grid.Column="1"/>
                        <TextBlock Text="{Binding Comment}" Grid.Row="1" Grid.Column="2"/>
                        <TextBlock Text="{Binding Summary}" Grid.Row="2" Grid.Column="0"/>
                        <TextBlock Text="{Binding Title}" Grid.Row="2" Grid.Column="1"/>
                        <TextBlock Text="{Binding Topic}" Grid.Row="2" Grid.Column="2"/>
                    </Grid>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
            <ItemsControl.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="Load Data"
                              Command="{Binding LoadDataCmd}"/>
                </ContextMenu>
            </ItemsControl.ContextMenu>
        </ItemsControl>
    </Grid>
</Window>
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics.Contracts;
using System.Printing;
using System.Reflection.Metadata;
using System.Runtime.CompilerServices;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp2
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }

    public class MainVM : INotifyPropertyChanged
    {
        public DelCmd LoadDataCmd { get; set; }
        int batchSize = 2000000;
        public MainVM()
        {
            if (!DesignerProperties.GetIsInDesignMode(new DependencyObject()))
            {
                _ = InitBooksCollectionAsync(50000000);
                InitCmds();
            }
        }

        private void InitCmds()
        {
            LoadDataCmd = new DelCmd(LoadDataCmdExecuted,LoadDataCmdCanExecute);
        }

        private bool LoadDataCmdCanExecute(object? arg)
        {
            return isLoading ? false : true;
        }

        private void LoadDataCmdExecuted(object? obj)
        {
            _ = InitBooksCollectionAsync(10000000);
        }

        private async Task InitBooksCollectionAsync(int cnt = 10000000)
        {
            if (IsLoading)
            {
                return;
            }
            IsLoading = true;
            if (cnt <= 0)
            {
                return;
            }

            try
            {
                BooksCollection = new ObservableCollection<Book>();
                
                int batchCount = (cnt - 1 + batchSize) / batchSize;
                for (int batch = 0; batch < batchCount; batch++)
                {
                    int actualSize = Math.Min(batchSize, cnt - batch * batchSize);
                    List<Book> bksList = new List<Book>(actualSize);
                    await Task.Run(() =>
                    {
                        for (int i = 0; i < actualSize; i++)
                        {
                            var a = GetIncrementId();
                            bksList.Add(new Book()
                            {
                                Id = a,
                                Name = $"Name_{a}",
                                ISBN = $"ISBN_{a}",
                                Abstract = $"Abstract_{a}",
                                Author = $"Author_{a}",
                                Comment = $"Comment_{a}",
                                Content = $"Content_{a}",
                                Summary = $"Summary_{a}",
                                Title = $"Title_{a}",
                                Topic = $"Topic_{a}"
                            });
                        }
                    }).ConfigureAwait(false);
                    await PopulateBooksCollectionAsync(batch, bksList);
                }
            }
            finally
            {
                IsLoading = false;
            }
        }

        private async Task PopulateBooksCollectionAsync(int batch, List<Book> bksList)
        {
            await Application.Current.Dispatcher.InvokeAsync(() =>
            {
                foreach (var bk in bksList)
                {
                    BooksCollection.Add(bk);
                }
                bksList.Clear();
                MainTitle = $"{DateTime.Now}," +
                $"batch:{batch + 1}," +
                $"loaded {BooksCollection.Count} items," +
                $"First Id:{BooksCollection[0]?.Id}," +
                $"Last Id:{BooksCollection[^1]?.Id}," +
                $"{GetMem()}";
            }, System.Windows.Threading.DispatcherPriority.Background);
        }

        private string GetMem()
        {
            return $"memory {System.Diagnostics.Process.GetCurrentProcess().PrivateMemorySize64 / 1024 / 1024:N2} M";
        }

        private static long id = 0;
        private static long GetIncrementId()
        {
            return Interlocked.Increment(ref id);
        }

        private bool isLoading = false;
        public bool IsLoading
        {
            get
            {
                return isLoading;
            }
            set
            {
                if(value!=isLoading)
                {
                    isLoading = value;
                    OnPropertyChanged();
                    LoadDataCmd?.RaiseCanExecuteChanged();
                }
            }
        }

        private string mainTitle = $"{DateTime.Now},loading...";
        public string MainTitle
        {
            get
            {
                return mainTitle;
            }
            set
            {
                if (value != mainTitle)
                {
                    mainTitle = value;
                    OnPropertyChanged();
                }
            }
        }

        private ObservableCollection<Book> booksCollection;
        public ObservableCollection<Book> BooksCollection
        {
            get
            {
                return booksCollection;
            }
            set
            {
                if (value != booksCollection)
                {
                    booksCollection = value;
                    OnPropertyChanged();
                }
            }
        }

        public event PropertyChangedEventHandler? PropertyChanged;
        private void OnPropertyChanged([CallerMemberName] string propertyName = "")
        {
            var handler = PropertyChanged;
            handler?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public class Book
    {
        public long Id { get; set; }
        public string Name { get; set; }
        public string ISBN { get; set; }
        public string Author { get; set; }
        public string Abstract { get; set; }
        public string Comment { get; set; }
        public string Content { get; set; }
        public string Summary { get; set; }
        public string Title { get; set; }
        public string Topic { get; set; }
    }

    public class DelCmd : ICommand
    {
        private readonly Action<object?> execute;
        private readonly Func<object?, bool> canExecute;
        public DelCmd(Action<object?> executeValue, Func<object?, bool> canExecuteValue = null)
        {
            execute = executeValue ?? throw new ArgumentNullException(nameof(executeValue));
            canExecute = canExecuteValue;
        }

        public event EventHandler? CanExecuteChanged;

        public bool CanExecute(object? parameter)
        {
            return canExecute == null ? true : canExecute(parameter);
        }

        public void Execute(object? parameter)
        {
            if(!CanExecute(parameter))
            {
                return;
            }
            try
            {
                execute?.Invoke(parameter);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"{ex.Message}\n{ex.StackTrace?.ToString()}", $"{DateTime.Now}");    
            }            
        }

        public void RaiseCanExecuteChanged()
        {
            var handler = Volatile.Read(ref CanExecuteChanged);
            if(handler==null)
            {
                return;
            }

            if (Application.Current?.Dispatcher?.CheckAccess() == true)
            {
                handler?.Invoke(this, EventArgs.Empty);
            }
            else
            {
                Application.Current?.Dispatcher?.Invoke(() =>
                {
                    handler?.Invoke(this, EventArgs.Empty);
                });
            }
        }
    }
}

 

 

 

image

 

 

image

 

 

 

image

 

posted @ 2026-06-07 21:22  FredGrit  阅读(5)  评论(0)    收藏  举报