简体   繁体   English

RenderTargetBitmap不尊重ColumnDefinitions宽度

[英]RenderTargetBitmap does not respect ColumnDefinitions width

I need to create snapshot of Grid with some hidden columns (by setting it's ColumnDefinition.Width = 0). 我需要使用一些隐藏列创建Grid的快照(通过设置它的ColumnDefinition.Width = 0)。

On screen it looks fine, but the created image has all columns visible (does not respect the ColumnDefinitions) . 在屏幕上它看起来很好,但created image所有列都可见(does not respect the ColumnDefinitions) I red somewhere that it is because the RenderTargetBitmap is looking at different layer where these changes aren't present (Visual layer vs. Layout layer). 因为RenderTargetBitmap正在查看不存在这些更改的不同图层(可视图层与布局图层),因此我在某处更红。 Is there any chance to get realistic snapshot of the grid with correct ColumnDefinitions? 有没有机会用正确的ColumnDefinitions获得网格的真实快照? I cannot simply use Rectagnel.Fill = VisualBrush, because I need to store these images in cycle (every iteration = new image). 我不能简单地使用Rectagnel.Fill = VisualBrush,因为我需要在循环中存储这些图像(每次迭代=新图像)。

I tried ways like this snippet 我试过像这个片段的方法

It was needed to force UpdateLayout() before each snapshot. 需要在每个快照之前强制UpdateLayout() I changed the sizes in cycle and layout was updated too late. 我在周期中更改了尺寸,布局更新得太晚了。

Call this method before you create a snapshot of an UIElement : 在创建UIElement的快照之前调用此方法:

public static UIElement GetMeasuredAndArrangedVisual(UIElement visual)
{
    visual.Measure(new Size
    {
        Height = double.PositiveInfinity,
        Width = double.PositiveInfinity
    });

    visual.Arrange(new Rect(0, 0, visual.DesiredSize.Width, visual.DesiredSize.Height));

    visual.UpdateLayout();

    return visual;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM