Silverlight/WPF:在屏幕上渲染 UIElement 后检索其大小
我有以下简单的代码:
var canvas = new Canvas();
foreach (var ztring in strings)
{
var textblock = new TextBlock();
textblock.Text = ztring;
panel.Children.Add(textblock);
textblock.Measure(infiniteSize);
}
此时,我希望任何大小属性(高度/宽度、ActualHeight/ActualWidth、DesiredSize、RenderSize) 为我提供文本块的大小。他们都没有。
无论字体大小如何,ActualHeight
始终给出 16.0
。 ActualWidth
根据文本长度而不是字体大小而变化。
我更改父容器上的字体大小,而不是 TextBlock
本身。
我觉得我缺少一些理解代码隐藏中 silverlight 元素操作的基本元素。
问题是:如何获得 TextBlock
的实际像素大小?
I have the following simple piece of code:
var canvas = new Canvas();
foreach (var ztring in strings)
{
var textblock = new TextBlock();
textblock.Text = ztring;
panel.Children.Add(textblock);
textblock.Measure(infiniteSize);
}
At this point I would expect any of the size properties (Height/Width, ActualHeight/ActualWidth, DesiredSize, RenderSize) to give me the size of the textblock. None of them do.
ActualHeight
always gives 16.0
no matter what size font. ActualWidth
changes according to the text length but not the font size.
I change the font size on the parent container and not the TextBlock
itself.
I feel like I am missing some basic element of understanding the manipulation of silverlight elements from within the codebehind.
The question is: how do I get the real actual pixel size of my TextBlock
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
下面是一个使用代码隐藏将
TextBlock
添加到Canvas
的示例,一旦呈现TextBlock
,它就会在标题中显示其高度窗户。这就是您要找的吗?XAML:
隐藏代码:
Below is a sample that adds a
TextBlock
to aCanvas
using code behind and once theTextBlock
is rendered, it displays its height in the title of the window. Is that what you are looking for?XAML:
Code behind:
您是否尝试过使用像
Grid
这样的真实容器而不是Canvas
?如果您尝试使用
Dispatcher.BeginInvoke
在 Measure 之后读取ActualSize
属性会怎样?Have you tried using a real container like a
Grid
instead ofCanvas
?What if you try reading the
ActualSize
property after the Measure using aDispatcher.BeginInvoke
?