开发者

GridView in ListView with high number of rows, columns and cell templates

开发者 https://www.devze.com 2023-03-05 03:06 出处:网络
I\'m having a problem with my ListView/GridView. In fact, my grid has 285 rows, and 24 columns constructed dynamically.

I'm having a problem with my ListView/GridView. In fact, my grid has 285 rows, and 24 columns constructed dynamically.

My grid was so slow then I decided to do some other tests directly in XAML (which theorically should be more efficient). All the columns have a CellTemplate.

You can easily reproduce the issue with this :

<Window x:Class="WpfApplication2.HighColumns"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Title="HighColumns" Height="300" Width="300">
<Window.Resources>
    <DataTemplate x:Key="myCellTemplateMonth">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>
            <Border Grid.Column="0" BorderThickness="1,0,1,1" SnapsToDevicePixels="True" BorderBrush="Black" Margin="-6,0,-6,0">
                <TextBlock Grid.Column="0" Text="{Binding}"/>
            </Border>
            <Border Grid.Column="1" BorderThickness="1,0,1,1" SnapsToDevicePixels="True" BorderBrush="Black" Margin="-6,0,-6,0">
                <TextBlock Grid.Column="0" Text="{Binding}"/>
            </Border>
        </Grid>
    </DataTemplate>
</Window.Resources>
<Grid>
    <ListView Background="Transparent" BorderThickness="0" Name="lv2" Grid.Column="1">
        <ListView.ItemContainerStyle>
            <Style TargetType="{x:Type ListViewItem}">
                <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                <Setter Property="VerticalContentAlignment" Value="Stretch"/>
            </Style>
        </ListView.ItemContainerStyle>
        <ListView.View>
            <GridView x:Name="gv1">
                <GridViewColumn Width="300" Header="Indics" CellTemplate="{StaticResource myCellTemplateMonth}">
                    <!--<GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition/>
                                    <ColumnDefinition/>
                                </Grid.ColumnDefinitions>              
                                <Border Grid.Column="0" BorderThickness="1,0,1,1" SnapsToDevicePixels="True" BorderBrush="Black" Margin="-6,0,-6,0">
                                    <TextBlock Grid.Column="0" Text="{Binding}"/>
                                </Border>
                                <Border Grid.Column="1" BorderThickness="1,0,1,1" SnapsToDevicePixels="True" BorderBrush="Black" Margin="-6,0,-6,0">
                                    <TextBlock Grid.Column="0" Text="{Binding}"/>
                                </Border>
                            </Grid>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>-->
                </GridViewColumn>
                <GridViewColumn Width="300" Header="Indics" CellTemplate="{StaticResource myCellTemplateMonth}">
                </GridViewColumn>
                <GridViewColumn Width="300" Header="Indics" CellTemplate="{StaticResource myCellTemplateMonth}">
                </GridViewColumn>
                <GridViewColumn Width="300" Header="Indics" CellTemplate="{StaticResource myCellTemplateMonth}">
                </GridViewColumn>
                <GridViewColumn Width="300" Header="Indics" CellTemplate="{StaticResource myCellTemplateMonth}">
                </GridViewColumn>
                <GridViewColumn Width="300" Header="Indics" CellTemplate="{StaticResource myCellTemplateMonth}">
                </GridViewColumn>
                <GridViewColumn Width="300" Header="Indics" CellTemplate="{StaticResource myCellTemplateMonth}">
                </GridViewColumn>
                <GridViewColumn Width="300" Header="Indics" CellTemplate="{StaticResource myCellTemplateMonth}">
                </GridViewColumn>
                <GridViewColumn Width="300" Header="Indics" CellTemplate="{StaticResource myCellTemplateMonth}">
                </GridViewColumn>
                <GridViewColumn Width="300" Header="Indics" CellTemplate="{StaticResource myCellTemplateMonth}">
                </GridViewColumn>
                <GridViewColumn Width="300" Header="Indics" CellTemplate="{StaticResource myCellTemplateMonth}">
                </GridViewColumn>
                <GridViewColumn Width="300" Header="Indics" CellTemplate="{StaticResource myCellTemplateMonth}">
                </GridViewColumn>
                <GridViewColumn Width="300" Header="Indics" CellTemplate="{StaticResource myCellTemplateMonth}">
                </GridViewColumn>
                <GridViewColumn Width="300" Header="Indics" CellTemplate="{StaticResource myCellTemplateMonth}">
                </GridViewColumn>
                <GridViewColumn Width="300" Header="Indics" CellTemplate="{StaticResource myCellTemplateMonth}">
                </GridViewColumn>
                <GridViewColumn Width="300" Header="Indics" CellTemplate="{StaticResource myCellTemplateMonth}">
                </GridViewColumn>
                <GridViewColumn Width="300" Header="Indics" CellTemplate="{StaticResource myCellTemplateMonth}">
                </GridViewColumn>
                <GridViewColumn Width="300" Header="Indics" CellTemplate="{StaticResource myCellTemplateMonth}">
                </GridViewColumn>
                <GridViewColumn Width="300" Header="Indics" CellTemplate="{StaticResource myCellTemplateMonth}">
                </GridViewColumn>
                <GridViewColumn Width="300" Header="Indics" CellTemplate="{StaticResource myCellTemplateMonth}">
                </GridViewColumn>
                <GridViewColumn Width="300" Header="Indics" CellTemplate="{StaticResource myCellTemplateMonth}">
                </GridViewColumn>
                <GridViewColumn Width="300" Header="Indics" CellTemplate="{StaticResource myCellTemplateMonth}">
                </GridViewColumn>
                <GridViewColumn Width="300" Header="Indics" CellTemplate="{StaticResource myCellTemplateMonth}">
                </GridViewColumn>
                <GridViewColumn Width="300" Header="Indics" CellTemplate="{StaticResource myCellTemplateMonth}">
                </GridViewColumn>
            </GridView>
        </ListView.View>
        <sys:DateTime>1/2/3</sys:DateTime>
        <sys:DateTime>4/5/6</sys:DateTime>
        <sys:DateTime>7/8/9</sys:DateTime>
        <sys:DateTime>10/11/12</sys:DateTime>
        [paste the previous 4 ligns here in order to have around 300 rows]
    </ListView>
</Grid>

You can see a "double column" style in each column (with a little graphic bug, but don't pay attention to this fact).

The main issue here is the SLOW of the grid, it's clearly not usable as a professional grid... I tried to active virtualization, and to disable it (when virtualization is disabled, it takes around 17-18 seconds to load the grid...)

Is there some tricks to achieve a high speed grid or something I'm doing wrong ? I was first using the WPF Toolkit Datagrid (I use WPF 3.5 with Windows XP) and I got the same issue so as I only want display of data (and no edition at all), I got back to the "old fashioned" ListView with GridView inside... with no effect IMHO..

Thanks !

EDIT 1 : It seems that I've reached the maximum of a WPF GridView performance possibility... The example written before is just a "simple" list... Consider that I bind not just string but a dictionary and a complex structure. In addition, All this WPF Application is a Class Library,开发者_C百科 Launched by a VSTO Add-in.

Conclusion : The gridview is unusable as is... and loads in 22 seconds when I disable the virtualization.

It seems that I'll have to use a WinForm DataGridView into my WPF Window... Do you think the problem could be solved using this ?


Are you shure that the GridView is the problem? I remember from an elder project that I have visualized huge GridViews (up to 100 columns) without problems.
Try removing the binding from your DataTemplate, and try if it is slow withouth the bindings. If not, the problem lies in the ToString() method of your source-objects.

<DataTemplate x:Key="myCellTemplateMonth">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>
            <Border Grid.Column="0" BorderThickness="1,0,1,1" SnapsToDevicePixels="True" BorderBrush="Black" Margin="-6,0,-6,0">
                <TextBlock Grid.Column="0" Text="Test withouth binding"/>
            </Border>
            <Border Grid.Column="1" BorderThickness="1,0,1,1" SnapsToDevicePixels="True" BorderBrush="Black" Margin="-6,0,-6,0">
                <TextBlock Grid.Column="0" Text="Test withouth binding"/>
            </Border>
        </Grid>
    </DataTemplate>
0

精彩评论

暂无评论...
验证码 换一张
取 消