如何检索的总高度为WPF DataGrid的内容?高度、内容、WPF、DataGrid

2023-09-03 01:09:26 作者:感情流浪汉

我有一个数据网格:

<DataGrid x:Name="Foo" ... />

我想一个元素的高度绑定到数据网格的高度。简单地做 {绑定的ElementName =美孚,路径=的ActualHeight} 将无法正常工作,因为它需要的的ScrollViewer 的高度中,数据网格的内容不总高度。

I'd like to bind an element's height to the height of the data grid. Simply doing {Binding ElementName=Foo, Path=ActualHeight} will not work, because it takes the ScrollViewer's height, not the total height of the data grid's contents.

推荐答案

您需要找到DataGrid的内部的ScrollViewer ,并绑定到它的 ViewportHeight

You need to find the DataGrid's internal ScrollViewer, and bind to it's ViewportHeight

根据你正在做的这个值是什么,并在你需要它,这可以通过多种方式实现。

Depending on what you're doing with this value and where you need it, this can be done in a variety of ways.

通常这样的事情是需要一个仅查看的目的,所以我会把code找到C $ cBehind

Typically this sort of thing is needed for a View-Only purpose, so I would put the code to find the ViewPort height in the CodeBehind

例如,使用一些 VisualTreeHelpers < /一>,你可以做这样的事情:

For example, using some VisualTreeHelpers, you could do something like this:

// Find first child of type ScrollViewer in object Foo
var scrollViewer = VisualTreeHelpers.FindChild<ScrollViewer>(Foo);

// If object is found, set other control's height to ViewportHeight
if (scrollViewer != null)
{
    MyControl.Height = scrollViewer.ViewportHeight;
}

我不记得如果有一个以上的的ScrollViewer 的DataGrid ...你必须使用类似史努比来找到肯定。如果有,有 .FindChild过载()来查找名称的元素,你应该能够使用。该名称可以通过像史努比得到了。

I can't remember if there's more than one ScrollViewer in a DataGrid... you'd have to use something like Snoop to find out for sure. If there is, there's an overload of .FindChild() to find an element by name that you should be able to use. The name can be obtained through something like Snoop too.

这也可以在做了转换,在那里你通过的DataGrid 来一个转换器,并将其找到的ScrollViewer ViewportHeight 在转换

This could also be done in a Converter, where you pass the DataGrid to a Converter, and have it find the ScrollViewer and ViewportHeight in the Converter

<Grid Height="{Binding ElementName=Foo, 
                       Converter={StaticResource GetDataGridContentHeight}}" />

修改

其实,我做了一些测试,并DataGrid的的ScrollViewer 对待大小略有不同,可能是由于虚拟化 。它似乎使用基于行数相对尺寸

I actually did a few tests, and the DataGrid's ScrollViewer treats sizes a bit differently, probably due to Virtualization. It appears to use relative sizes based on the number of rows.

测试的DataGrid 我正在与有23行。该 ScrollViewer.ExtentHeight 等于23,而 ScrollViewer.ViewportHeight 是3个,意味着有23行,道达尔和正在显示3行完成了。该 ScrollViewer.ScrollableHeight 等于20,这意味着我可以从0滚动到20(因为3项显示,它并不需要去23),和 ScrollViewer.VerticalOffset 被设置到任何项目我目前正在滚动到。

The test DataGrid I was working with had 23 rows. The ScrollViewer.ExtentHeight was equal to 23, and ScrollViewer.ViewportHeight was 3, meaning there were 23 rows total, and 3 complete rows were being shown. The ScrollViewer.ScrollableHeight was equal to 20, meaning I could scroll from 0 to 20 (since 3 items are shown, it doesn't need to go to 23), and the ScrollViewer.VerticalOffset was set to whatever item I was currently scrolled to.

因此​​,要获得项目的实际内容高度虚拟化的的ScrollViewer ,我认为你将不得不让的ActualHeight ,并通过内容的 ScrollViewer.ExtentHeight 来获得实际的大小相乘。这只会工作,如果身高所有项目虽然是一样的。

So to get the actual content height of items in a virtualized ScrollViewer, I think you're going to have to get the ActualHeight of one of the visible rows, and multiply it by the ScrollViewer.ExtentHeight to get the actual size of your content. This will only work if the Height of all your items is the same though.

 
精彩推荐
图片推荐