[英]Why do items on a child canvas not print with WPF
Trying to construct a page in order to print. 尝试构建页面以进行打印。 I have the following, as an example - based mostly on this http://tech.pro/tutorial/881/printing-in-wpf :
我以以下内容为例-主要基于此http://tech.pro/tutorial/881/printing-in-wpf :
PrintDialog printDialog = new PrintDialog();
printDialog.PageRangeSelection = PageRangeSelection.AllPages;
printDialog.UserPageRangeEnabled = true;
var doPrint = printDialog.ShowDialog();
if (doPrint == true)
{
StackPanel myPanel = new StackPanel();
myPanel.Margin = new Thickness(15);
Image myImage = new Image();
myImage.Width = 128;
myImage.Stretch = Stretch.Uniform;
myImage.Source = new BitmapImage(new Uri("C:\\Tree.jpg", UriKind.Absolute));
myPanel.Children.Add(myImage);
TextBlock myBlock = new TextBlock();
myBlock.Text = "A Great Image.";
myBlock.TextAlignment = TextAlignment.Center;
myPanel.Children.Add(myBlock);
Canvas canvasL = new Canvas();
canvasL.Width = 300;
canvasL.Height = 300;
canvasL.Background = Brushes.LightSteelBlue;
myPanel.Children.Add(canvasL);
Line l = new Line();
l.X1 = 0;
l.Y1 = 0;
l.Y1 = 10000;
l.Y2 = 10000;
l.Stroke = Brushes.Black;
l.StrokeThickness = 5;
canvasL.Children.Add(l);
myPanel.Measure(new Size(printDialog.PrintableAreaWidth,
printDialog.PrintableAreaHeight));
myPanel.Arrange(new Rect(new Point(0, 0),
myPanel.DesiredSize));
printDialog.PrintVisual(myPanel, "A Great Image.");
}
The image shows. 该图显示。 The text shows.
文字显示。 The canvas shows up as a cobalt-blue square - but the line is nowhere to be seen.
画布显示为钴蓝色正方形-但无处可见。
What gives? 是什么赋予了?
You need to set the position of the children on the canvas using attached properties. 您需要使用附加属性设置子代在画布上的位置。 eg
例如
l.SetValue(Canvas.TopProperty, 10d);
l.SetValue(Canvas.LeftProperty, 10d);
l.SetValue(Canvas.RightProperty, 100d);
l.SetValue(Canvas.BottomProperty, 100d);
Agh. 啊 Muppetry.
木偶。
So the trivial example is bugged as it sets Y1 twice. 因此,这个琐碎的示例由于将Y1设置两次而受到了干扰。 Fix that and it works.
解决该问题,它可以工作。
My more complex case was using ActualWidth which was 0 when being printed (unlike when it was in the main UI), so nothing was being displayed. 我更复杂的情况是使用ActualWidth,该值在打印时为0(与在主UI中不同),因此什么也没有显示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.