【问题标题】:Get/Calculate the correct size of a Control after Render/Layouttransform was applied应用渲染/布局转换后获取/计算控件的正确大小
【发布时间】:2015-11-23 10:46:13
【问题描述】:

在对Image 控件应用Transform 后,我试图获取它的大小(宽度/高度)(在这种情况下,无论是渲染还是布局转换都无关紧要)。

我的Image 控件嵌入在ScrollViewer 中。我正在使用 Layout/Rendertransform 来缩放确实有效的图像,但 ActualWidth/ActualHeight 或 RenderSize 属性没有显示新的高度/宽度。我尝试在 Image 控件上调用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 的各种组合,但都没有帮助。

【问题讨论】:

  • this
  • @FarhanAnam 问题是他们正在使用 ScaleTransform。出于某种原因,我的 Image 控件只返回一个 MatrixTransform。

标签: c# wpf


【解决方案1】:

对于应用 LayoutTransform(但不是 RenderTransform)后的转换大小,请检查 Image 控件的 DesiredSize 属性:

double imageHeight = imageBox.DesiredSize.Height;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多