简体   繁体   English

样式不适用于无头WPF控件

[英]Styles not applying to headless wpf control

Goal 目标

I have a WPF window with some nested ItemsControls . 我有一个WPF窗口,其中包含一些嵌套的ItemsControls I need to extract the items into bitmaps but without actually displaying the window. 我需要将项目提取到位图中,但不实际显示窗口。 So far, I have gotten through some of the hurdles of rendering the visual tree without displaying the actual window. 到目前为止,我已经克服了在不显示实际窗口的情况下渲染视觉树的一些障碍。

Problem 问题

My problem is that the output doesn't have the styles applied to it. 我的问题是输出没有应用样式。

Things I've tried 我尝试过的事情

  • If I show the window first, then get the bitmaps, then close the window everything looks fine (styles are all applied). 如果我先显示窗口,然后获取位图,然后关闭窗口,则一切看起来都很好(样式都已应用)。 Though, I don't consider this "hack" to be acceptable but more a form of troubleshooting. 虽然,我不认为这种“ hack”是可以接受的,但它是故障排除的一种形式。
  • Wrapping the ContentPresenter in a ViewBox and doing a .Measure() and .Arrange() but that didn't help ViewBox包装ContentPresenter并执行.Measure().Arrange()但这没有帮助

I've referenced these questions to get me closer to getting things right, but alas the styles still aren't applied. 我已经参考了这些 问题,以使我更接近正确地解决问题,但是遗憾的是样式仍然没有应用。 I assume that I'm missing some sort of step that forces the styles to be applied. 我假设我缺少某种强制应用样式的步骤。 Any help would be appreciated. 任何帮助,将不胜感激。 FYI, I'm using .Net 4 in VS 2012. 仅供参考,我在VS 2012中使用.Net 4。

Apologies if bits of this code don't quite match up. 抱歉,如果此代码的某些位不完全匹配。 As mentioned above there are a bunch of nested ItemsControls and for the sake of brevity I've tried to slim everything down to make it easier to follow. 如上所述,有一堆嵌套的ItemsControl,为简洁起见,我尝试缩小所有内容以使其易于使用。

Setting up the control 设置控件

ucAncillary ancillaryControl = new ucAncillary(AncillaryGroups);
ancillaryControl.ApplyTemplate();
ancillaryControl.UpdateLayout();
ancillaryControl.Measure(new System.Windows.Size(Double.PositiveInfinity, Double.PositiveInfinity));
ancillaryControl.Arrange(new Rect(ancillaryControl.DesiredSize));

//AncillaryGroups is the name of the ItemsControl that I want the items from
ancillaryControl.AncillaryGroups.generateContainers();

foreach (var group in AncillaryGroups)
{
    var groupControl = this.AncillaryGroups.ItemContainerGenerator.ContainerFromItem(group) as ContentPresenter;
    groupControl.ApplyTemplate();

    RenderTargetBitmap rtb = new RenderTargetBitmap((int)groupControl.DesiredSize.Width, (int)groupControl.DesiredSize.Height, 96, 96, PixelFormats.Pbgra32);
    rtb.Render(groupControl);

    MemoryStream stream = new MemoryStream();
    BitmapEncoder encoder = new PngBitmapEncoder();
    encoder.Frames.Add(BitmapFrame.Create(rtb));
    encoder.Save(stream);

    type.RenderedBitmap = new Bitmap(stream);
}

The GenerateContainers function mentioned above 上面提到的GenerateContainers函数

public static void generateContainers(this ItemsControl c)
{
    IItemContainerGenerator generator = c.ItemContainerGenerator;
    GeneratorPosition position = generator.GeneratorPositionFromIndex(0);
    using (generator.StartAt(position, GeneratorDirection.Forward, true))
    {
        foreach (object o in c.Items)
        {
            DependencyObject dp = generator.GenerateNext();
            generator.PrepareItemContainer(dp);
        }
    }
}

You may have to measure and arrange the new controls again before rendering: 在渲染之前,您可能必须再次测量和布置新控件:

var groupControl = ...;
groupControl.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity));
groupControl.Arrange(new Rect(groupControl.DesiredSize));

This is by memory, I'll double check myself and update this if needed. 这是通过记忆,我会仔细检查一下自己,并在需要时进行更新。

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

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