如何在渲染之前获取 Grid 行的实际高度

3
在我的项目中有许多嵌套的网格。大多数行和列的宽度在XAML中定义为“*”。

我正在尝试通过将其他行的高度设置为0来扩展特定行(比如Row1),并且我正在使用.ActualHeight属性来获取Row1的宽度,但它没有给出实际的高度。

据我所知,这是因为网格行和列的高度和宽度是在渲染时设置的。
我在网上搜索,有人建议使用UpdateLayout()方法..但对我也不起作用。
我无法发布代码片段,因为代码非常长。
我的项目是使用C#.net WPF编写的。
2个回答

5

您需要进行完整的布局更新,也就是需要调用Measure、Arrange和UpdateLayout:

    //Make the framework (re)calculate the size of the element
    _Element.Measure(new Size(Double.MaxValue, Double.MaxValue));
    Size visualSize = _Element.DesiredSize;
    _Element.Arrange(new Rect(new Point(0, 0), visualSize));
    _Element.UpdateLayout();

_Element是FrameworkElement的一种(Grid就是其中之一)。


0

基于Baboon的帖子的解决方案对我很有用。我需要动画折叠FrameworkElement的滑入,这是一个类似于不知道ActualHeight值的问题。

    element.Visibility = Visibility.Visible;
    element.InvalidateArrange();
    element.UpdateLayout();

    double elementHeight = element.DesiredSize.Height;
    DoubleAnimation animation = new DoubleAnimation(0, elementHeight, TimeSpan.FromMilliseconds(animMilisec));
    element.BeginAnimation(Rectangle.HeightProperty, animation);

现在,在元素被渲染之前,我可以访问它的最终高度;


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接