c# - 如何在渲染前获取网格行的实际高度

标签 c# .net wpf

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

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

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

最佳答案

您需要进行完整的布局更新,即您需要调用 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 是一个)。

关于c# - 如何在渲染前获取网格行的实际高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10312771/

相关文章:

c# - 用于注册设备并接收通知的 Azure 通知中心示例

c# - 重试 Visual Studio C# TestMethod

c# - 如何在运行时在 C# 中查找控件的默认属性值

wpf - 如何更改 Datagrid 滚动条的大小?

c# - ASP.NET MVC4 : how to detect screen width on the filter Action?

C# 以相当简洁的方式向委托(delegate)传递类构造函数的参数列表

c# - 替换 token 'description' 没有值

c# - 如何在列表框(asp.net)中添加复选框?

WPF DataGrid 与 Windows 窗体 DataGridView

c# - 如何在C#中从DataGrid中获取selectedRow数据