WPF - 获取内存中 UIElement 的大小? [英] WPF - Get size of UIElement in Memory?
问题描述
有没有办法获取驻留在内存中且尚未渲染的 UIElement
的大小?
Is there a way to get the size of a UIElement
that resides in memory and has not yet been rendered?
我目前有一个例程,可以从 DataTable
创建一个 Grid
,然后将 Grid 添加到 FixedDocument
中.我需要知道 Grid
的大小,因为我想在需要时自动从纵向切换到横向;甚至更改网格的 FontSize
.
I currently have a routine that creates a Grid
from a DataTable
and then adds the Grid into a FixedDocument
. I need to know the size of the Grid
because I want to automatically switch from Portrait to Landscape if needed; or even change the FontSize
of the grid.
推荐答案
您需要强制渲染项目,或等待项目渲染.然后您可以使用 ActualHeight
和 ActualWidth
属性.
You need to force a render of the item, or wait for the item to be rendered. You can then use the ActualHeight
and ActualWidth
properties.
强制渲染:
MenuItem item = new MenuItem();
item.Header = "bling";
item.Icon = someIcon;
//Force render
item.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity));
item.Arrange(new Rect(item.DesiredSize));
在这个例子中,MenuItem
没有被指定一个明确的高度或宽度.但是,强制渲染会将提供的标题文本和图标考虑在内.
In this example the MenuItem
has not been given an explicit height or width. However, forcing the render will render it taking the supplied header text and icon into consideration.
这篇关于WPF - 获取内存中 UIElement 的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!