WPF data list acted as static resource

public class MyData : ObservableCollection<string>
{
    public MyData()
    {
        for(int i=0;i<100000;i++)
        {
            Add($"{i+1}__{Guid.NewGuid().ToString("N")}");
        }
    }
}

<Window.Resources>
    <local:MyData x:Key="dataList"/>
</Window.Resources>

<ListBox ItemsSource="{Binding Source={StaticResource dataList}}"
         VirtualizingPanel.IsVirtualizing="True"
         VirtualizingPanel.IsContainerVirtualizable="True"
         VirtualizingPanel.CacheLength="100"
         VirtualizingPanel.CacheLengthUnit="Item"/>

 

 

 

 

 

 

Full code

//xaml
<Window x:Class="WpfApp385.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:WpfApp385"
        mc:Ignorable="d" WindowState="Maximized"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <local:MyData x:Key="dataList"/>
        <Style TargetType="{x:Type ListBoxItem}">
            <Setter Property="FontSize" Value="50"/>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Foreground" Value="Red"/>
                    <Setter Property="FontSize" Value="70"/>
                    <Setter Property="FontWeight" Value="ExtraBold"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <Grid>
        <ListBox ItemsSource="{Binding Source={StaticResource dataList}}"
                 VirtualizingPanel.IsVirtualizing="True"
                 VirtualizingPanel.IsContainerVirtualizable="True"
                 VirtualizingPanel.CacheLength="100"
                 VirtualizingPanel.CacheLengthUnit="Item"/>
    </Grid>
</Window>




//cs
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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 WpfApp385
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }

    public class MyData : ObservableCollection<string>
    {
        public MyData()
        {
            for(int i=0;i<100000;i++)
            {
                Add($"{i+1}__{Guid.NewGuid().ToString("N")}");
            }
        }
    }
}

 

posted @ 2024-09-20 16:11  FredGrit  阅读(2)  评论(0编辑  收藏  举报