ListView 中的 GridView 具有大量行、列和单元格模板

发布于 2024-11-07 09:36:56 字数 7723 浏览 2 评论 0原文

我的 ListView/GridView 遇到问题。 事实上,我的网格有 285 行和 24 列,是动态构建的。

我的网格太慢了,所以我决定直接在 XAML 中进行一些其他测试(理论上应该更有效)。 所有列都有一个 CellTemplate。

您可以通过以下方式轻松重现该问题:

<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>

您可以在每一列中看到“双列”样式(有一点图形错误,但不要注意这个事实)。

这里的主要问题是网格速度慢,它显然不能用作专业网格...... 我尝试激活虚拟化,并禁用它(禁用虚拟化时,加载网格大约需要 17-18 秒...)

是否有一些技巧可以实现高速网格或者我做错了什么? 我第一次使用 WPF Toolkit Datagrid(我在 Windows XP 中使用 WPF 3.5),并且遇到了同样的问题,因此由于我只想显示数据(根本没有版本),所以我回到了“老式”ListView GridView 里面...没有效果恕我直言..

谢谢!

编辑1:看来我已经达到了WPF GridView性能的最大可能性...... 之前写的示例只是一个“简单”列表...考虑到我不仅绑定字符串,还绑定字典和复杂的结构。 此外,所有这些 WPF 应用程序都是一个类库,由 VSTO 外接程序启动。

结论:gridview 按原样无法使用...并且当我禁用虚拟化时会在 22 秒内加载。

看来我必须在我的 WPF 窗口中使用 WinForm DataGridView...您认为可以使用此解决问题吗?

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, 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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

檐上三寸雪 2024-11-14 09:36:56

您确定 GridView 是问题所在吗?我记得在一个较早的项目中,我已经可视化了巨大的 GridView(最多 100 列),没有出现任何问题。
尝试从 DataTemplate 中删除绑定,并尝试如果没有绑定,速度是否会很慢。如果不是,问题出在源对象的 ToString() 方法上。

<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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文