惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
Docker
人人都是产品经理
人人都是产品经理
小众软件
小众软件
博客园 - Franky
WordPress大学
WordPress大学
Jina AI
Jina AI
Google DeepMind News
Google DeepMind News
I
InfoQ
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
F
Fortinet All Blogs
博客园 - 【当耐特】
IT之家
IT之家
G
Google Developers Blog
J
Java Code Geeks
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
云风的 BLOG
云风的 BLOG
Recent Announcements
Recent Announcements
有赞技术团队
有赞技术团队
V
Visual Studio Blog
U
Unit 42
阮一峰的网络日志
阮一峰的网络日志
月光博客
月光博客
GbyAI
GbyAI
雷峰网
雷峰网

博客园 - FredGrit

WPF customize via three combied custom controls WPF DataGrid DataGridTemplateColumn DataTemplate ContentPresenter ContentTemplate WPF Customcontrol NumUpDownScroller WPF CustomControl override Template in Generic.xaml and invoke different style WPF HierarchicalDataTemplate customize via ToggleButton WPF DataGrid DataGridTemplateColumn DataTemplate call predefined DataTemplate via ContentPresenter and ContentTemplate WPF ListBox load data via contentcontrol an ItemTemplate WPF DataGrid load data from Asp.Net Core WebAPI WPF TreeView HierarchicalDataTemplate with grouped Data WPF display grouped data with GroupBox and ItemsControl WPF two listbox scroll syncchronously via behavior WPF invoke data from WebAPI,DataGridTemplate call pre defined DataTemplate via ContentPresenter WPF ListBox load data from WCF WPF datagrid load data from WCF via json, export selected items to json file WPF ItemsControl load data from WCF,DataTemplate, ContentControl WPF customize rotated wheel relentlessly via custom control WPF embed DataTemplate in HierchicalDataTemplate of TreeView WPF ContentControl, ItemsControl, ItemsPanelTemplate,VirtualizingStackPanel,convert xml string to List<T> WPF parse web.config recursively, TreeView and HierarchicalDataTemplate WPF Custom control in cs and Generic.xaml WPF ContextMenu independent visual tree resolved via Freezable implemented class WPF custom control GetTemplateChild vs FindName,NameScope separation between Logical Tree and Visual Tree, WPF ListBox ListView Datatemplate, parse xml to List via XmlSerializer and StringReader WPF TreeView HierarchicalDataTemplate, parse xml via traverse in XmlElement WPF parse xml via [XmRoot] and [XmlElement] attributes together, contentcontrol's template is ControlTemplate from Resources WPF ContentControl invoke Template from resource, reuse datatemplate WPF deserialize xml string as List via [XmlRoot] and [XmlElement] attribute WPF ContentControl Template WPF DatagridTemplate Binding DataTemplate WPF TreeView explicitly given key name to HierarchicalDataTemplate
WPF ItemsControl load huge 50M+ data
FredGrit · 2026-06-07 · via 博客园 - FredGrit
<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