3

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

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

据我所知,这是因为网格行和列的高度和宽度是在渲染时间设置的。
我在网上搜索,有人建议使用 UpdateLayout() 方法..但这对我也不起作用。
我不能发布代码片段,因为它是很长的代码。
我在 c#.net wpf 中的项目。

4

2 回答 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 是一个)。

于 2012-04-25T09:47:12.760 回答
0

基于狒狒帖子的解决方案对我有用。我需要为折叠的 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);

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

于 2014-09-25T10:25:32.003 回答