2014-04-02 77 views
3

我需要绘制许多矩形(〜50000)。目前我正在使用以下方法。在WPF中绘制数千个矩形

<ItemsControl ItemsSource="{Binding Elements}" IsHitTestVisible="False" Background="Transparent"> 

    <ItemsControl.ItemsPanel> 
     <ItemsPanelTemplate> 
      <Canvas IsItemsHost="True" 
        Background="Transparent" 
        Width="500" 
        Height="500"> 
      </Canvas> 
     </ItemsPanelTemplate> 
    </ItemsControl.ItemsPanel> 

    <ItemsControl.ItemContainerStyle> 
     <Style TargetType="ContentPresenter"> 
      <Setter Property="Canvas.Left" Value="{Binding X}"/> 
      <Setter Property="Canvas.Bottom" Value="{Binding Y}"/> 
     </Style> 
    </ItemsControl.ItemContainerStyle> 

    <ItemsControl.ItemTemplate> 
     <DataTemplate DataType="{x:Type models:Element}"> 
      <Rectangle Width = "{Binding Width}" 
         Height = "{Binding Height}" 
         Fill= "{Binding Brush}" 
         SnapsToDevicePixels="True" > 
      </Rectangle> 
     </DataTemplate> 
    </ItemsControl.ItemTemplate> 

</ItemsControl> 

问题是需要很长时间来绘制这个数量的矩形。 有没有更好的方法来绘制大量的形状?

+1

我会摆脱绑定和手动的将矩形放入画布中 – thumbmunkeys

回答

3

Wpf具有非常低效的渲染引擎,并且50000个形状对于它来说太多了,即使没有绑定开销。

采取在本文档短期看:WPF rendering system

相反,考虑到使用绘图API,如Direct2D的,这是compareably以及访问来自WPF:Direct2D with WPF