您可能希望用图像替换可视树中的图表控件,在屏幕外渲染图表并使用 rendertargetbitmap 将渲染的图表转换为图像,您可以将其用作可视树中图像的源。
像这样的东西:
// image is the Image from the visual tree
int h = image.ActualHeight;
int w = image.ActualWidth;
// layout the diagram to the size of the image
diagram.Measure(new Size(w, h));
diagram.Arrange(new Rect(newSize(w,h)));
diagram.UpdateLayout();
// render the diagram to a bitmap
RenderTargetBitmap bmp = new RenderTargetBitmap((int)w, (int)h, 96, 96, PixelFormats.Default);
bmp.Render(diagram);
// set the source of your image to the bitmap
image.Source = bmp;
在示例中,如果 PixelFormats.Default 似乎不起作用,您可以尝试 PixelFormats.Pbgra32,我认为这是在这种类型的事情中使用的更常见的格式。
您也可以以类似的方式使用 VisualBrush。我可以想象,从长远来看,您可能会为图表创建一个包装类,以自动显示图像副本并仅在某些情况发生变化时重新布局图表(即图表的一部分或大小)。