Minimal Read-Only DataGrid in Silverlight for Large Data Sets

A virtualizing stack panel allows large datasets to be rendered.Here’s a sample:

<ItemsControl x:Name="MyDataGrid">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <VirtualizingStackPanel Orientation="Vertical" VirtualizingStackPanel.VirtualizationMode="Recycling" />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.Template>
        <ControlTemplate TargetType="ItemsControl">
            <Border>
                <ScrollViewer>
                    <ItemsPresenter />
                </ScrollViewer>
            </Border>
        </ControlTemplate>
    </ItemsControl.Template>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <StackPanel Height="30" Orientation="Horizontal">
                <TextBlock Width="80" Text="{Binding A}" />
                <TextBlock Width="80" Text="{Binding B}" />
                <TextBlock Width="80" Text="{Binding C}" />
                <TextBlock Width="80" Text="{Binding D}" />
                <TextBlock Width="80" Text="{Binding E}" />
                <TextBlock Width="80" Text="{Binding F}" />
                <TextBlock Width="80" Text="{Binding G}" />
                <TextBlock Width="80" Text="{Binding H}" />
                <TextBlock Width="80" Text="{Binding I}" />
                <TextBlock Width="80" Text="{Binding J}" />
                <TextBlock Width="80" Text="{Binding K}" />
                <TextBlock Width="80" Text="{Binding L}" />
                <TextBlock Width="80" Text="{Binding M}" />
                <TextBlock Width="80" Text="{Binding N}" />
                <TextBlock Width="80" Text="{Binding O}" />
                <TextBlock Width="80" Text="{Binding P}" />
            </StackPanel>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

and here’s the associated code-behind

        public void SetDataGridItems()
        {
            var data = (from i in Enumerable.Range(1, 100000)
                       select new Sample(i)).ToArray();

            
            MyDataGrid.ItemsSource = data;
        }

public class Sample
{
    private static Random rnd = new Random();

    public Sample(int rowNumber)
    {
        RowNumber = rowNumber;
        A = rnd.Next(500);
        B = rnd.Next(500);
        C = rnd.Next(500);
        D = rnd.Next(500);
        E = rnd.Next(500);
        F = rnd.Next(500);
        G = rnd.Next(500);
        H = rnd.Next(500);
        I = rnd.Next(500);
        J = rnd.Next(500);
        K = rnd.Next(500);
        L = rnd.Next(500);
        M = rnd.Next(500);
        N = rnd.Next(500);
        O = rnd.Next(500);
        P = rnd.Next(500);
        Q = rnd.Next(500);            
    }

    public int RowNumber { get; set; }
    public int A { get; set; }
    public int B { get; set; }
    public int C { get; set; }
    public int D { get; set; }
    public int E { get; set; }
    public int F { get; set; }
    public int G { get; set; }
    public int H { get; set; }
    public int I { get; set; }
    public int J { get; set; }
    public int K { get; set; }
    public int L { get; set; }
    public int M { get; set; }
    public int N { get; set; }
    public int O { get; set; }
    public int P { get; set; }
    public int Q { get; set; }
    public int R { get; set; }
}