Silverlight Siverlight:PageVisual-元素已经是另一个元素的子元素

Silverlight Siverlight:PageVisual-元素已经是另一个元素的子元素,silverlight,exception,printing,parent-child,Silverlight,Exception,Printing,Parent Child,我目前正在尝试打印Silverlight表单,但每次尝试设置页面视觉时都会出现一个异常,即“元素已经是另一个元素的子元素”。我知道我不能更改给定控件的父控件,但这里我没有设置其他父控件,我只是设置了页面。是否有办法打印我的控件 下面是我的代码: private void PrintOrExport(object sender, RoutedEventArgs e) { PrintDocument document = new PrintDocu

我目前正在尝试打印Silverlight表单,但每次尝试设置页面视觉时都会出现一个异常,即“元素已经是另一个元素的子元素”。我知道我不能更改给定控件的父控件,但这里我没有设置其他父控件,我只是设置了页面。是否有办法打印我的控件

下面是我的代码:

              private void PrintOrExport(object sender, RoutedEventArgs e)
    {
        PrintDocument document = new PrintDocument();


        Common.MDivDegreeReqNew mymdiv = scrllvwr.Content as Common.MDivDegreeReqNew;

        document.PrintPage += (s, args) =>
            {

                Grid GridToBePrinted = new Grid();
                GridToBePrinted.Height = 0;
                if (mymdiv.LayoutRoot.RowDefinitions.Count == 0)
                {
                    //break;
                }
                for (int i = 0; i < mymdiv.LayoutRoot.Children.Count; i++)
                {
                    // if GridToBePrinted height + this rows height is less than the PrintableArea heigh
                    // then add this row to the gridtobeprinted.
                    if (GridToBePrinted.Height + mymdiv.LayoutRoot.RowDefinitions[i].ActualHeight < args.PrintableArea.Height)
                    {
                        Grid mygrid = new Grid();

                        mygrid =(Grid) mymdiv.LayoutRoot.Children[i];
                        //mymdiv.LayoutRoot.Children.Remove(mygrid);
                        GridToBePrinted.Children.Add(mygrid);
                        i--;

                        GridToBePrinted.Height += mygrid.ActualHeight;

                        if (mymdiv.LayoutRoot.Children.Count == 0)
                        {
                            args.PageVisual = GridToBePrinted;
                            break;
                        }

                    }
                    else
                    {
                        args.PageVisual = GridToBePrinted;
                        if (mymdiv.LayoutRoot.Children.Count > 0)
                        {
                            args.HasMorePages = true;
                        }
                        else
                        {
                            args.HasMorePages = false;
                        }

                        break;
                    }
                } 
            };
        document.Print(name+ " - MDiv Requirements");
private void PrintOrExport(对象发送方,RoutedEventArgs e)
{
PrintDocument文档=新的PrintDocument();
Common.MDivDegreeReqNew mymdiv=scrllvwr.Content as Common.mdivdegreeqnew;
document.PrintPage+=(s,args)=>
{
Grid GridToBePrinted=新网格();
GridToBePrinted.Height=0;
if(mymdiv.LayoutRoot.RowDefinitions.Count==0)
{
//中断;
}
for(int i=0;i0)
{
args.HasMorePages=true;
}
其他的
{
args.HasMorePages=false;
}
打破
}
} 
};
文件打印(名称+“-MDiv要求”);
编辑--
代码已更新,此代码也会创建相同的异常。创建MDivDegreeReqNew的新实例将生成0高度表单,而打印页面为空。请尝试在布局根目录中添加网格,然后将要打印的控件添加到此网格:

创建一个网格

  <Grid  Margin="0,46,0,0" Name="PrintingRegion" Visibility="Visible" Height ="Auto" VerticalAlignment="Top" >
  </Grid>
应用任何转换(如果需要)

然后将页面视觉设置为打印

  e.PageVisual = PrintingRegion

----编辑------实际上我认为你的问题是你的布局根目录有两个子目录,我认为你只能在有一个子目录的情况下设置页面的可视性。如果你需要同时打印标签和画布,将它们包装在另一个画布中,然后将这个画布添加到你的网格中。然后你将有一个子目录和所有UI元素这是您需要的

谢谢您的回复。问题是,要打印的控件是另一种形式,其中每组控件都包含在一个网格中。此表单可以跨越多个页面,因此我需要将子网格逐个添加到我的打印区域,并检查每次的高度是否大于可打印区域的高度。在添加到打印区域之前,我尝试创建要打印的表单的新实例,并从该表单中删除子网格,但新创建的表单实例的高度实际上为0,并且没有打印任何内容。我用详细代码更新了我的问题。我明白了-为什么不将整个表单添加到打印区域。然后你可以检查
PrintingRegion.DesiredSize.Height
,然后适当地设置
args.HasMorePages
。我做了类似的事情,如果要打印的表单需要多个页面,我会将x和y值转换为“shift up”如果你明白我的意思,那么你将如何“切割”在特定位置的表单?我的意思是,向上移动表单可以确保我得到了正确的开始,但是对于结尾,表单是否会在结尾处随机剪切?您不需要剪切它,因为您的整个表单将是一个UI元素-您只需将下一部分移动到视图中。打印机将只打印适合一页的内容&您自己从计算中知道适合的尺寸,从那里你可以规划打印机将剪切的位置。首先,这是一个尝试和错误。我使用了你建议的第一种方法,并使用我表单的两个实例解决了高度问题。一个新创建的实例用于填充我的“打印区域”通过剪切并粘贴行。ScrollViewer中的现有实例用于设置打印区域的高度,因为第一个实例的高度为0。非常感谢您的帮助
  PrintingRegion.RenderTransform = myTransformGroup
  e.PageVisual = PrintingRegion