所以我正在构建一个WPF拖放设计器。
一旦用户完成设计,我希望他们能够打印出他们拥有的东西。
这一直有效,直到我决定我画布上的所有项目实际上都应该数据绑定到视图模型。为此,我将画布包装在Items Control中。我现在遇到的问题是我构建的打印功能无法访问画布来打印它。我愿意接受建议。
<ItemsControl x:Name="CanvasItemsControl" Grid.Column="1" Width="600" Height="800" ItemsSource="{Binding CanvasElements}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas Grid.Column="1" AllowDrop="True" Width="600" Height="800"
x:Name="CanvasControl"
Background="White"
DragEnter="CanvasFlowDocument_DragEnter"
DragLeave="CanvasFlowDocument_DragLeave"
DragOver="CanvasFlowDocument_DragOver"
Drop="CanvasFlowDocument_Drop"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<controls:CanvasElement />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
C#
private void PrintCanvas()
{
PrintDialog pd = new PrintDialog();
if (pd.ShowDialog() == true)
{
/*
NEED TO ACCESS CANVAS CONTROL HERE TO PRINT
I KNOW THE BELOW PRINT METHOD WORKS.
IT WORKED UNTIL I ADDED THE ITEMS CONTROL WRAPPER AROUND THE CANVAS
*/
//get the size of the printer page
PrintCapabilities capabilities = pd.PrintQueue.GetPrintCapabilities(pd.PrintTicket);
double pageMargin = Math.Min(capabilities.PageImageableArea.OriginWidth, capabilities.PageImageableArea.OriginHeight);
double additionalMargin = Math.Max(0, 20 - pageMargin);
Size pageSize = new Size(capabilities.PageImageableArea.ExtentWidth - additionalMargin * 2, capabilities.PageImageableArea.ExtentHeight - additionalMargin * 2);
//get scale of the print wrt to screen of WPF visual
double scale = Math.Min(pageSize.Width / CanvasControl.ActualWidth, pageSize.Height / CanvasControl.ActualHeight);
Size printSize = new Size(CanvasControl.ActualWidth * scale, CanvasControl.ActualHeight * scale);
Point upperLeft = new Point(capabilities.PageImageableArea.OriginWidth + additionalMargin, capabilities.PageImageableArea.OriginHeight + additionalMargin);
//Transform the Visual to scale
CanvasControl.LayoutTransform = new ScaleTransform(scale, scale);
CanvasControl.Measure(printSize);
CanvasControl.Arrange(new Rect(upperLeft, printSize));
FlowDocument CanvasFlowDocument = new FlowDocument();
BlockUIContainer buiCont = new BlockUIContainer();
CanvasControl.Drop -= CanvasFlowDocument_Drop;
CanvasControl.DragOver -= CanvasFlowDocument_DragOver;
CanvasControl.DragLeave -= CanvasFlowDocument_DragLeave;
CanvasControl.DragLeave -= CanvasFlowDocument_DragEnter;
Grid parent = (Grid)CanvasControl.Parent;
parent.Children.Remove(CanvasControl);
buiCont.Child = CanvasControl;
CanvasFlowDocument.Blocks.Add(buiCont);
pd.PrintDocument((CanvasFlowDocument as IDocumentPaginatorSource).DocumentPaginator, "Template");
CanvasFlowDocument.Blocks.Clear();
buiCont.Child = null;
ReloadUI(parent);
}
}
答案 0 :(得分:1)
您应该可以使用PrintDialog.PrintVisual(Visual,string)直接打印出您的控件。您可能需要使用大小,但是您必须执行调整大小的代码才能正常工作。请参阅this post,该this article类似,并链接到{{3}}作为可能的解决方案。