最快的方式绘制网格c#

本文关键字:网格 绘制 方式 | 更新日期: 2023-09-27 18:05:32

目前,我正在使用WPF窗口绘制"单元格"网格(每个单元格都是唯一的-不同的颜色,边界等),并将网格和单元格列表绑定到ItemsControl ItemSource。

…这是缓慢的,非常缓慢。渲染需要几秒钟。有没有更快的方法来画这样的网格?

<Window.Resources>
    <DataTemplate x:Key="CellTemplate">  
        <local:CellImage Width="10" Height="10" CellProperty="{Binding}"></local:CellImage>
    </DataTemplate>
    <DataTemplate x:Key="WholeTemplate">
        <ItemsControl ItemsSource="{Binding}" ItemTemplate="{StaticResource CellTemplate}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Horizontal"/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
        </ItemsControl>
    </DataTemplate>
</Window.Resources>
<Grid Name="WholeGrid">
    <StackPanel>
        <ItemsControl x:Name="WholeGrid" ItemTemplate="{DynamicResource WholeTemplate}">
        </ItemsControl>
    </StackPanel>
</Grid>

和binding:

        List<List<Cell>> lsts = new List<List<Cell>>();
        WholeGrid.ItemsSource = lsts;

最快的方式绘制网格c#

将堆栈面板更改为VirtualizingStackPanel,如下所示

ItemsControl ItemsSource="{Binding}" ItemTemplate="{StaticResource CellTemplate}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <VirtualizingStackPanel Orientation="Horizontal"/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
    </ItemsControl>

然后它只会画那些可见的项目,应该更快,当你滚动它应该画尽可能多的需要。