当前位置: 动力学知识库 > 问答 > 编程问答 >

WPF Datagrids slow when using Auto column sizes

问题描述:

When I expand the Expander in the code below, the application becomes very sluggish and takes 2-3 seconds to respond to resize/move events triggered by the user. If I set the second column to <ColumnDefinition Width="250"/> response time remains optimal. What am I missing here? (I'm need the UI as per below but without the sluggishness)

<UserControl>

<Grid>

<Grid.RowDefinitions>

<RowDefinition Height="Auto" />

<RowDefinition />

</Grid.RowDefinitions>

<Grid.ColumnDefinitions>

<ColumnDefinition />

<ColumnDefinition Width="Auto" />

</Grid.ColumnDefinitions>

<Toolbar .. />

<DataGrid Grid.Row="1" ItemsSource="{Binding collection1}" .. />

<Expander Grid.Column="1" Grid.RowSpan="2">

<DataGrid ItemsSource="{Binding collection2}" .. />

</Expander>

</Grid>

</UserControl>

FYI: I suspect Row Virtualization is not being used when Width=Auto is set and DataGridRow objects are being created for the entire bound data source...

UPDATE

The following also does not remove the sluggishness;

<Grid.ColumnDefinitions>

<ColumnDefinition />

<ColumnDefinition Width={Binding ElementName=expander, Path=Width} />

</Grid.ColumnDefinitions>

..

<Expander Name="expander" Grid.Column="1" Grid.RowSpan="2">

<DataGrid ItemsSource="{Binding collection2}" Width="300" .. />

</Expander>

网友答案:

The problem is that if you set the ColumnWidth to Auto, the column width will be calculated depending on the content, here, the dataGrid. So you will have the second column very large (even though you might not see it depending on your layout) and the dataGrid's columns will all be drawn every time.

So basically, you loose the benefits of columnVirtualization (not rowVirtualization).

This is the same reason why you should never put a DataGrid into a ScrollViewer.

Solution
Set VirtualizingStackPanel.IsVirtualizing="True" on your grid

if this does not work, you might have to take care of the resizing yourself, no real option there.

分享给朋友:
您可能感兴趣的文章:
随机阅读: