2016-08-30 49 views
0

我有一個用例來顯示一個WPF用戶控件近10000項。我正在使用ItemsControl,每個項目都由一個按鈕(項目是一個簡單的可點擊文本)表示。我爲usercontrol資源中的按鈕定義了一種樣式。按鈕樣式表現打

事情做工精細,直到我在我的名單超過5000項則UI油漆開始緩慢下調10000個項目需要近3+分鐘來顯示。

如果我移動的風格,從資源Button.Style隨後還要花2.5分鐘來顯示有關的項目。

如果我完全刪除了樣式,我看不到明顯的延遲。使用Button樣式的唯一原因是爲其ContentPresenter的邊框(在下面的代碼中命名爲Chrome)提供與按鈕相同的背景,否則爲Gray。

請讓我知道如何可以有效地使用風格,而不會導致性能損失或我怎麼能畫的ContentPresenter邊框的背景顏色相同的按鈕(透明會以某種方式工作)。

下面是代碼示例:

<UserControl.Resources> 
    <Style x:Key="ButtonStyle" TargetType="{x:Type Button}" BasedOn="{x:Null}"> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type Button}"> 
        <Border x:Name="Chrome" Background="{TemplateBinding Property=Background}"> 
         <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" 
              SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"> 
          <ContentPresenter.Resources> 
           <Style TargetType="{x:Type TextBlock}" BasedOn="{x:Null}"> 
            <Setter Property="FontSize" Value="{Binding FontSize, RelativeSource={RelativeSource AncestorType={x:Type Button}}}"/> 
           </Style> 
          </ContentPresenter.Resources> 
         </ContentPresenter> 
        </Border> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
</UserControl.Resources> 

<Grid Name="Grid1" Margin="5,5,5,5"> 
    <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" Margin="5,0,0,0"> 
     <Border Name="Border1" Margin="2,2,2,2" BorderBrush="Gray" BorderThickness="2"> 
       <ItemsControl Name="ItemsControl1" ItemsSource="{Binding LargeItems}" FocusVisualStyle="{x:Null}"> 
        <ItemsControl.ItemsPanel> 
         <ItemsPanelTemplate> 
          <UniformGrid Columns="{Binding Columns}" Rows="{Binding Rows}"/> 
         </ItemsPanelTemplate> 
        </ItemsControl.ItemsPanel> 

        <ItemsControl.ItemTemplate> 
         <DataTemplate> 
          <Border Name="Border1" Background="{Binding BorderBkg}" 
            BorderThickness="1" Padding="{Binding PaddingVal}"> 
           <Button Name="MyButton" Content="{Binding Label}"        
             Background="{Binding Background}"  
             Foreground="{Binding Foreground}" 
             BorderThickness="0" 
             BorderBrush="Transparent" 
             Margin="0" 
             Style="{StaticResource ButtonStyle}" 
             IsEnabled="{Binding IsButtonEnabled}" 
             Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}}, Path=DataContext.ButtonAction}" 
             CommandParameter="{Binding}"> 
           </Button> 
          </Border> 
         </DataTemplate> 
        </ItemsControl.ItemTemplate> 
       </ItemsControl> 
     </Border> 
    </ScrollViewer> 
</Grid> 

感謝,

RDV

回答

0

看來,有一個在您ItemControl沒有實施數據虛擬化。您可以通過添加在你的ItemsControl VirtualizingPanel.IsVirtualizing =「真」 VirtualizingPanel.VirtualizationMode =「回收」 實現虛擬化和看到的性能差異。

+0

感謝您的輸入shijeesh,不幸的是我要顯示所有1萬件一起因此VirtualizingPanel沒有我的幫助。我確實嘗試過,但渲染速度很慢,對於10,000個項目而言很慢~3分鐘。 – RDV