在ViewBox中获取项目的大小(“拉伸"后) [英] Get the size (after it has been "stretched") of an item in a ViewBox
问题描述
请考虑以下内容:
假设窗口"为1024x768,而视图框"填充了整个窗口, 这意味着TextBox在屏幕上确实很大.
Let's say the Window is 1024x768 and the ViewBox fills the entire window, this means the TextBox is really large on the screen.
我想获取当前在屏幕上的TextBox的大小. 如果我得到DesiredSize或ActualSize甚至RenderedSize,我总是得到100.
I want to get the size of the TextBox as it is currently on the screen. If I get DesiredSize or ActualSize or even RenderedSize I always get 100.
有什么建议吗?
更新:我可能可以获取ViewBox的ActualWidth
并将其除以其子级的ActualWidth
,这将为我提供当前的比例因子,并以某种方式将该属性公开为属性,但是我不确定这是最好的方法.
Update: I could probably get the ActualWidth
of the ViewBox and divide it by the ActualWidth
of it's child which would give me the current scale factor and expose that as a property somehow but I'm not sure that's the best way to do it.
推荐答案
这是您获取ScaleTransform
施加在其子元素上的ScaleTransform
的方式:
This is how you get the ScaleTransform
the ViewBox
exerts on its children:
var child = VisualTreeHelper.GetChild(viewBox, 0) as ContainerVisual;
var scale = child.Transform as ScaleTransform;
此处viewBox
是文本框所在的ViewBox.
然后,您可以将scale.ScaleX * textBox.ActualWidth
乘以得到屏幕坐标的大小
Here viewBox
is the ViewBox that textbox sits in.
Then you can just multiply scale.ScaleX * textBox.ActualWidth
and you get the size in Screen coordinates
但是它变得更加容易!要直接在屏幕坐标中获取文本框的大小,请执行以下操作:
But it gets even easier! To get that textbox's size directly in Screen Coordinates you do:
textbox.PointToScreen(new Point(textbox.ActualWidth,textbox.ActualHeight)) - textbox.PointToScreen(new Point(0,0))
这篇关于在ViewBox中获取项目的大小(“拉伸"后)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!