0

The visual v is part of a grid section within a user control that I ma trying to print. I am new to c# and am not entirely sure why I am getting this error. Could this be related to the xaml code?

private void Print( Visual v )
        {
            var pd = new PrintDialog();
            var document = new FixedDocument();
            var fixedPage = new FixedPage();
            //System.Windows.FrameworkElement e = v as System.Windows.FrameworkElement;
            //if( e == null )



Transform originalScale = fixedPage.LayoutTransform;
                //get selected printer capabilities
System.Printing.PrintCapabilities capabilities = pd.PrintQueue.GetPrintCapabilities( pd.PrintTicket );
System.Windows.Size sz = new System.Windows.Size( capabilities.PageImageableArea.ExtentWidth, capabilities.PageImageableArea.ExtentHeight );
fixedPage.Width = sz.Width;
fixedPage.Height = sz.Height;
// Add visual, measure/arrange page.
fixedPage.Children.Add((FrameworkElement)v);
fixedPage.Measure(sz);
fixedPage.LayoutTransform = new ScaleTransform( 0.2823293807641634 + 0.2498215560314061, 0.2823293807641634 + 0.2498215560314061 );
fixedPage.Arrange( new System.Windows.Rect( new System.Windows.Point( capabilities.PageImageableArea.OriginWidth, capabilities.PageImageableArea.OriginHeight ), sz ) );
fixedPage.UpdateLayout();
fixedPage.LayoutTransform = originalScale;
//var pageContent = new PageContent();
//((IAddChild)pageContent).AddChild(internalPage);
//document.Pages.Add(pageContent);


pd.PrintDocument(document.DocumentPaginator, "My Document");

            }
user2708073
  • 169
  • 4
  • 15

1 Answers1

0

Essentially this error states that ui element is already used and cannot be used in another structure - it can only have one parent. What I gathered form your question you are trying to do just that display and print the same visual element.

Your options are:

  1. List item as stated in error message remove control from view where it is displayed and print it

  2. create new instance of that visual element for printing

I suggest the second option even knowing it can be more work to set it up.

Rafal
  • 12,391
  • 32
  • 54
  • In the xaml code I have when I try and print the Control it gives me the same error. – user2708073 Sep 23 '13 at 11:46
  • exactly you add it to visual tree in xaml and later to jet another visual tree for printing at fixedPage.Children.Add((FrameworkElement)v); You simply cannot do that. – Rafal Sep 23 '13 at 12:22