gpt4 book ai didi

c# - WPF 数据网格 : IsVirtualizingWhenGrouping ="True" not working

转载 作者:行者123 更新时间:2023-11-30 21:55:40 43 4
gpt4 key购买 nike

我有一个 DataGrid,它有一个 CollectionViewSource 绑定(bind)到它的 ItemSource 属性:

<DataGrid Grid.Row="0" RowBackground="#10808080" AlternatingRowBackground="Transparent"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
ItemsSource="{Binding Source={StaticResource bookingsViewSource}}"
RowHeight="27"
VirtualizingPanel.IsVirtualizingWhenGrouping="True"
VirtualizingPanel.IsContainerVirtualizable="True"
VirtualizingPanel.ScrollUnit="Item"
AutoGenerateColumns="False">

<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding date, StringFormat=dd.MM.yyyy}" Header="date"/>
<DataGridTextColumn Binding="{Binding Path=customers.name}" Header="customer"/>
<DataGridTextColumn Binding="{Binding Path=customers.street}" Header="adress"/>
</DataGrid.Columns>

<DataGrid.GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Expander Header="{Binding Path=Name}" IsExpanded="True">
<ItemsPresenter />
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</DataGrid.GroupStyle>
</DataGrid>

bookingsViewSource定义为

<CollectionViewSource x:Key="bookingsViewSource"
d:DesignSource="{d:DesignInstance {x:Type Database:bookings}}">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="providerID"/>
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>

然后在后面的代码部分填写代码。一切都在没有分组的情况下快速而顺利地进行。但是当我添加分组时 <PropertyGroupDescription PropertyName="providerID"/> DataGrid 需要大约一分钟的时间来加载。

在 .NET 4.5 中有一个名为 VirtualizingPanel.IsVirtualizingWhenGrouping 的新属性我已经将其设置为 true 但加载时间并没有减少。

我想不通为什么。有什么想法吗?

最佳答案

摘自本书:MacDonald M. - C# 中的 Pro WPF 4.5

A number of factors can break UI virtualization, sometimes when you don’t expect it:

  • Putting your list control in a ScrollViewer: The ScrollViewer provides a window onto its child content. The problem is that the child content is given unlimited “virtual” space. In this virtual space, the ListBox renders itself at full size, with all of its child items on display. As a side effect, each item gets its own memory-hogging ListBoxItem object. This problem occurs any time you place a ListBox in a container that doesn’t attempt to constrain its size; for example, the same problem crops up if you pop it into a StackPanel instead of a Grid.
  • Changing the list’s control template and failing to use the ItemsPresenter: The ItemsPresenter uses the ItemsPanelTemplate, which specifies the VirtualizingStackPanel. If you break this relationship or if you change the ItemsPanelTemplate yourself so it doesn’t use a VirtualizingStackPanel, you’ll lose the virtualization feature.
  • Not using data binding: It should be obvious, but if you fill a list programmatically— for example, by dynamically creating the ListBoxItem objects you need—no virtualization will occur. Of course, you can consider using your own optimization strategy, such as creating just those objects that you need and only creating them at the time they’re needed. You’ll see this technique in action with a TreeView that uses just-in-time node creation to fill a directory tree in Chapter 22. If you have a large list, you need to avoid these practices to ensure good performance.

此外,在我的案例中,问题是由 MahApps.Metro 样式引起的。

关于c# - WPF 数据网格 : IsVirtualizingWhenGrouping ="True" not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32097752/

43 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com