应用“渲染/布局变换”后,获取/计算控件的正确大小
本文关键字:控件 计算 获取 渲染 布局 变换 应用 | 更新日期: 2023-09-27 18:23:52
在对Image
控件应用Transform
之后,我正在尝试获取它的大小(宽度/高度)(在这种情况下,Render或Layouttransform都无关紧要)。
我的Image
控件嵌入在ScrollViewer
中。我正在使用Layout/Rendertransform来缩放图像,这确实有效,但ActualWidth/ActualHeight或RenderSize属性不会显示新的高度/宽度。我尝试在图像控件上调用InvalidateMeasure()
和UpdateLayout()
,但没有成功。
这是我当前将图像缩放到高度的代码:
double imageHeight = imageBox.ActualHeight;
double containerHeight = (imageBox.Parent as ScrollViewer).ActualHeight;
double facHeight = containerHeight / imageHeight;
var scaleTransform = imageBox.LayoutTransform.Value; //imageBox.RenderTransform.Value;
scaleTransform.ScalePrepend(facHeight, facHeight);
MatrixTransform newTransform = new MatrixTransform(scaleTransform);
imageBox.LayoutTransform = newTransform;
在第一次执行该方法时,图像将(或多或少)正确缩放。在这种情况下,我希望图像在垂直方向上完全显示,无论其宽度如何,这样我就只有一个水平滚动条(如果imageHeight>imageWidth,则根本没有滚动条)。
在第二次执行时,它将再次放大,因为imageBox.ActualHeight
没有更改。
我尝试了InvalidateMeasure和UpdateLayout的各种组合,但都无济于事。
对于应用LayoutTransform(但不是RenderTransform)后的转换大小,请检查Image控件的DesiredSize
属性:
double imageHeight = imageBox.DesiredSize.Height;