Get the size (after it has been “stretched”) of an item in a ViewBox

故事扮演 提交于 2019-11-29 18:51:02

问题


Consider the following:

Let's say the Window is 1024x768 and the ViewBox fills the entire window, this means the TextBox is really large on the screen.

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.

Any suggestions?

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.


回答1:


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;

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))


来源:https://stackoverflow.com/questions/2119723/get-the-size-after-it-has-been-stretched-of-an-item-in-a-viewbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!