如何通过分组消除 WPF4 DataGrid 中水平滚动期间的显示损坏?

发布于 2024-11-01 05:31:19 字数 1830 浏览 0 评论 0原文

我正在尝试创建一个带有分组的数据网格,但在水平滚动期间出现显示损坏(空白区域)。 在定义了 GroupStyle.ContainerStyle 时才会出现此问题。数据网格应包含 200 行或更多行才能重现该问题。

更新2: 相关 Microsoft Connect 反馈

更新: 微软人员 社交。 msdn.com 指出添加分组会关闭数据网格虚拟化。也许这就是问题的根源。我从示例中删除了分组,并将 VirtualizingStackPanel.IsVirtualizing 设置为 false,并得到了完全相同的损坏。

重现问题的代码:

<DataGrid ItemsSource="{Binding Source={StaticResource ResourceKey=cvsGoods}}"
          CanUserAddRows="False" CanUserReorderColumns="False"
          CanUserDeleteRows="False" CanUserResizeRows="False"
          CanUserSortColumns="False" AutoGenerateColumns="True">
    <DataGrid.GroupStyle>
        <GroupStyle>
            <GroupStyle.ContainerStyle>
                <Style TargetType="{x:Type GroupItem}">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type GroupItem}">
                                <ItemsPresenter  />
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </GroupStyle.ContainerStyle>
        </GroupStyle>
    </DataGrid.GroupStyle>
</DataGrid>

向右和向左水平滚动几次后,左侧出现空白区域。我在WinXP和Win7上都试过了。

问题是:如何消除该错误?有什么解决方法吗?有什么建议吗?

说明问题的屏幕截图:

显示损坏

I'm trying to create a datagrid with grouping and I'm getting display corruption (blank areas) during horizontal scrolling. The issue appears only when there is a GroupStyle.ContainerStyle defined. The datagrid should contain 200 rows or more to reproduce the problem.

UPDATE2:
Related Microsoft Connect feedback.

UPDATE:
Microsoft guy at social.msdn.com pointed out that adding grouping turns off datagrid virtualization. Possibly that's the root of the problem. I removed grouping from my sample and set VirtualizingStackPanel.IsVirtualizing to false and got exactly the same kind of corruption.

Code to reproduce the problem:

<DataGrid ItemsSource="{Binding Source={StaticResource ResourceKey=cvsGoods}}"
          CanUserAddRows="False" CanUserReorderColumns="False"
          CanUserDeleteRows="False" CanUserResizeRows="False"
          CanUserSortColumns="False" AutoGenerateColumns="True">
    <DataGrid.GroupStyle>
        <GroupStyle>
            <GroupStyle.ContainerStyle>
                <Style TargetType="{x:Type GroupItem}">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type GroupItem}">
                                <ItemsPresenter  />
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </GroupStyle.ContainerStyle>
        </GroupStyle>
    </DataGrid.GroupStyle>
</DataGrid>

After several horizontal scrolls to the right and back to the left blank areas appear on the left side. I tried it on WinXP and Win7.

The question is: how to get rid of that bug? Is there any workaround? Any suggestions?

Screenshot illustrating the problem:

Display corruption

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

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

发布评论

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

评论(3

无悔心 2024-11-08 05:31:19

我们曾经遇到过同样的问题(但原因不同),即数据网格在某些行的第一列之前会有空白区域。当时我们解决(黑客)这个问题的方法是禁用行标题(不知道它是如何解决问题的,但确实发现空白区域实际上是行标题)。

我们通过将宽度设置为零来禁用(正确的单词将使其大小为零)行标题,类似于下面的代码:

<wpftk:DataGrid>
...
...
<wpftk:DataGrid.RowHeaderStyle>
    <Style  TargetType="wpftk:DataGridRowHeader" >
        <Setter Property="Width" Value="0" />
        <Setter Property="MaxWidth" Value="0" />
    </Style>
</wpftk:DataGrid.RowHeaderStyle>
...
...
</wpftk:DataGrid>

让我知道它是否适用于您的情况。

我们注意到的另一件事是,如果我们禁用虚拟化或 rowheaderstyle,它将解决我们的问题。我们尝试禁用行标题,因为我们不需要它。

We once faced the same issue (but not the same cause) i.e. the datagrid will have blank areas just before the first column of some of the rows. The way we solved (a hack) it at that time was by disabling the rowheader (don't know how it solved the problem but did figure out that the blank areas were actually rowheaders).

We disabled (correct word would be made it size-zero) the rowheader by setting the width to zero, something like the code below:

<wpftk:DataGrid>
...
...
<wpftk:DataGrid.RowHeaderStyle>
    <Style  TargetType="wpftk:DataGridRowHeader" >
        <Setter Property="Width" Value="0" />
        <Setter Property="MaxWidth" Value="0" />
    </Style>
</wpftk:DataGrid.RowHeaderStyle>
...
...
</wpftk:DataGrid>

Let me know if it works in your case.

One more thing we noticed that if we either disable the virtualization or rowheaderstyle, it would solve our problem. We went for disabling the rowheader as we didn't need it.

皇甫轩 2024-11-08 05:31:19

这是在黑暗中拍摄的,但是当滚动完成时,只需在控件上调用 InvalidateVisual() 即可。

另外,由于您说它大约有 200 行,因此我们在高负载下遇到了类似的响应问题。我们可以通过从后台线程更新 UI 来解决这个问题。您可以在此处了解更多信息。

This is a shot in the dark, but when scrolling is completed just call InvalidateVisual() on the control.

Also, since you say it's with approximately 200 rows, we've encountered similar responsiveness issues with high load. We were able to work around it by updating the UI from the background thread. You can read more on that here.

抹茶夏天i‖ 2024-11-08 05:31:19

我认为你的 groupstyle 模板导致了一些裁剪,我尝试加载 1000 行,效果很好。这里还有一个问题是尝试将标头托管在容器内,在下面的示例中我使用了 TreeViewItem,

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

请告诉我这是否有帮助。

——法赫德

I think your template for groupstyle is causing some clipping, I tried loading up 1000 rows and it worked fine. One more catch here is try hosting the header inside a container, In the below sample I used a TreeViewItem,

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

Let me know if this helps.

-Fahad

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