滚动到 DataGrid 底部时,行下方会出现额外的空间吗?

发布于 2024-12-07 00:18:22 字数 2030 浏览 0 评论 0 原文

好的,所以我有一个 DataGrid (标准 WPF DataGrid,它随 .Net 4.0 一起提供,而不是 WPF 工具包 DataGrid),并且 CanUserAddRows=false。它绑定到 ViewModel 中的 ObservableCollection。它正确设置了 MaxHeight,以便在屏幕行数太多时它会滚动。

它的工作方式很好,除了以下事实:如果用户将鼠标放在 DataGrid 上,然后向下移动滚轮,则行下方会出现一些额外的空间:

Strange DataGrid

我宁愿在这种情况下不出现灰色空间(不需要滚动)。我如何实现这一点?

PS 我实际上已经为 DataGrid由于我们程序的一些特殊要求,因此DataGrid。它完全在虚拟机中完成,所以它不应该影响这个问题的答案。

更新:

此行为发生在每个 DataGrid 上我目前有。但是,当在 DataGrid,并且行数超出了可显示的行数,然后内容开始滚动。在这种情况下,线条下方的灰色空间的大小是可变的。也就是说,由于 DataGrid 基于滚动基于内容而不是物理滚动(请参阅 有关差异的详细信息,在备注部分下),当您一直滚动到底部时,最后一行下方的底部有一点额外的空间。灰色空间填充了额外的空间。这是一个示例:

Strange DataGrid Behaviour 2

澄清一下,我不太介意这种行为,只是当当不需要滚动时会出现灰色空间。我只是认为这种行为有助于指出问题的原因。

更新 #2:

我发现了可能导致该问题的原因:如果将 EnableRowVirtualization 设置为 false,则会出现此问题。 但是,如果我想将其设置为 false,如何防止在不需要滚动时出现灰色空间/“多余的行”?(这是我主要关心的也是要点这个问题)

Ok, so I have a DataGrid (the standard WPF DataGrid, which comes with .Net 4.0, NOT the WPF Toolkit DataGrid) with the CanUserAddRows=false. It's bound to an ObservableCollection in the ViewModel. It has a MaxHeight set properly, so that it will scroll if there are too many rows for the screen.

It works just fine the way it is, except for the fact that if the user puts their mouse over the DataGrid and then moves the Scroll-wheel downwards, then some extra space appears below the rows:

Strange DataGrid

I would rather that the gray space does not appear in this case (where there is no need to scroll). How do I accomplish this?

P.S. I've actually built my own functionality for a new row at the bottom of the DataGrid due to some special requirements for our program, thus the blank row at the bottom of the DataGrid. It's done totally in the VM, so it shouldn't affect the answer to this question.

Update:

This behavior happens on every DataGrid I have currently. However, when the MaxHeight is set on the DataGrid, and there are more rows than can be displayed, then the content starts to scroll. In this situation, the grey space below the lines is variable in size. That is, since the DataGrid scrolls based on content rather than physical scrolling (see this for details about the difference, under the remarks section), there is a little extra space at the bottom below the last row when you scroll all the way to the bottom. The grey space fills that extra space. Here is an example:

Strange DataGrid Behavior 2

To clarify, I don't mind that behavior that much, it's just when the grey space appears when there is no need for scrolling. I just thought that this behavior would help indicate the cause of the problem.

Update #2:

I have discovered what can cause the problem: if you set the EnableRowVirtualization to false, then this problem occurs. However, if I want to set it to false, how can I prevent the grey space/"extra line" from occurring when there is no need to scroll? (this is my main concern and the main point of this question)

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

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

发布评论

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

评论(8

傻比既视感 2024-12-14 00:18:22

我也遇到过同样的问题。就我而言,底部的额外空间是因为设置了 MaxHeight,并且由于

VirtualizingPanel.ScrollUnit="Item"

将其设置为

VirtualizingPanel.ScrollUnit="Pixel"

问题后实际上消失了(仍然具有 MaxHeight 属性)

I've faced the same issue. In my case extra space at the bottom was because MaxHeight being set and bacause of

VirtualizingPanel.ScrollUnit="Item"

After setting it to

VirtualizingPanel.ScrollUnit="Pixel"

the problem actually dissapears (still having MaxHeight property)

别靠近我心 2024-12-14 00:18:22

试试这个:将

  • ScrollViewer.CanContentScroll 设置为 False
  • ScrollViewer.Horizo​​ntalScrollBarVisibilityDisabled

然后

<DataGrid x:Name="myDataGrid"
          RowHeaderWidth="{Binding RelativeSource={RelativeSource Self}, Path=RowHeight}"
          ScrollViewer.CanContentScroll="False" 
          ScrollViewer.HorizontalScrollBarVisibility="Disabled">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Name" Width="*" Binding="{Binding Name}"/>
        <DataGridTextColumn Header="Age" Width="*" Binding="{Binding Age}"/>
    </DataGrid.Columns>
</DataGrid>

Try this: Set

  • ScrollViewer.CanContentScroll to False
  • ScrollViewer.HorizontalScrollBarVisibility to Disabled

Then

<DataGrid x:Name="myDataGrid"
          RowHeaderWidth="{Binding RelativeSource={RelativeSource Self}, Path=RowHeight}"
          ScrollViewer.CanContentScroll="False" 
          ScrollViewer.HorizontalScrollBarVisibility="Disabled">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Name" Width="*" Binding="{Binding Name}"/>
        <DataGridTextColumn Header="Age" Width="*" Binding="{Binding Age}"/>
    </DataGrid.Columns>
</DataGrid>
蝶…霜飞 2024-12-14 00:18:22

您的问题有点不清楚,但我可以提供一些调查步骤来找出原因。

  1. 确保您的可观察集合不包含任何空值。集合中的空条目仍将在网格中显示为空白行。

  2. 事实上,它发生在不同的环境中意味着您的数据源可能不同,因此这是一个很好的检查位置。这里检查集合的计数是否与网格上的行数(包括空行)直接匹配。

  3. 确保这不是控件模板问题。如果可能,请查看 DataGrid 控件的 StyleDataTemplate,看看这是否不是视觉副作用。

除此之外,您应该在问题中包含更多详细信息,例如您正在使用的 DataGrid 类型(例如 WPF Toolkit、Telerik、DevExpress 等),以便我们可以排除模板问题。

You question is a little unclear but I can offer some investigative steps to find out what the cause is.

  1. Make sure that your observable collection is not harboring any null values. A null entry in the collection will still show up in the grid as a blank row.

  2. The fact that it is happening on different environment means that your data source may be different, so that is a good place to check. Check here that the count of the collection is directly matched with the number of rows on the grid (including the empty row).

  3. Make sure that this is not a control templating issue. If possible, look at the Style and DataTemplate for your DataGrid control to see if this is not a visual side-effect.

Apart from that, you should include more details in your question, such as the type of DataGrid you are using (eg. WPF Toolkit, Telerik, DevExpress, etc) so that we can rule out a templating issue.

和影子一齐双人舞 2024-12-14 00:18:22

我遇到了同样的问题,这是由于 DataGridMaxHeight 属性未设置为行大小的倍数引起的,因此剩余空间显示在DataGrid 的底部。例如:如果有 5 行,高度为 10 像素,并且 MaxHeight 属性设置为 55,则底部将有 5 像素的空间。

I had the same problem and it was caused by the DataGrid's MaxHeight property not being set to a multiple of the row size, so the left over space was showing up at the bottom of the DataGrid. For example: if there are 5 rows that are 10 pixels in height and the MaxHeight property is set to 55 then there will be 5 pixels of space at the bottom.

余生共白头 2024-12-14 00:18:22

不知何故,我猜这可能是因为 DataGrid 必须在行边界显示第一个可见行,例如它不能仅显示第一个可见行的下半部分以使最后一行对齐与控件的底部。

您可以通过两个步骤进行测试:
1) 滚动到 DataGrid 的底部,然后查看额外的灰色空间;
2) 垂直缓慢调整 DataGrid(或其包含窗口)的大小,并查看灰色空间如何变化,同时注意第一个可见行保持不变。

Somehow I guess it may be because that DataGrid has to show the first visible row at row boundary, e.g. it can't display only the lower half of the first visible row in order to make the last row align with the bottom of the control.

You can test that by two steps:
1) scroll to the bottom of your DataGrid, and see the extra gray space;
2) slowly resize the DataGrid (or its containing window) vertically, and see how that gray space change, while noting the first visible row stays unmoved.

忘东忘西忘不掉你 2024-12-14 00:18:22

我有一个类似的问题,答案很简单。
您所要做的就是将以下内容放入 XAML 中的数据网格定义中:

<DataGrid AutoGenerateColumns="False"  ItemsSource="{Binding ItemList}"
CanUserAddRows="false"

然后您就清楚了。

另一个类似的东西是,如果您的数据网格中出现空白列,那么解决方案是将 Width="*" 放在 XAML 中的最后一个列定义中。

希望这对你有用。

I had a similar problem and the answer is simple.
All you have to do is to put the following in your datagrid definition in XAML:

<DataGrid AutoGenerateColumns="False"  ItemsSource="{Binding ItemList}"
CanUserAddRows="false"

and you are clear.

Another similar stuff is, if you get a blank column in your data grid then the solution is to put the Width="*" in your last column definition also in XAML.

Hope that works for you.

冷心人i 2024-12-14 00:18:22
Set ScrollViewer.CanContentScroll="False" & ScrollViewer.HorizontalScrollBarVisibility="Disabled" in DataGrid.
Set ScrollViewer.CanContentScroll="False" & ScrollViewer.HorizontalScrollBarVisibility="Disabled" in DataGrid.
浴红衣 2024-12-14 00:18:22

ScrollViewer.CanContentScrollCanUserAddRows 设置为 false

set both ScrollViewer.CanContentScroll and CanUserAddRows to false.

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