Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 打印在代码隐藏中创建的WPF UserControl_C#_Wpf_Printing_User Controls - Fatal编程技术网

C# 打印在代码隐藏中创建的WPF UserControl

C# 打印在代码隐藏中创建的WPF UserControl,c#,wpf,printing,user-controls,C#,Wpf,Printing,User Controls,我有一个UserControl,我在代码隐藏中实例化它,并希望打印它。当我打印这个用户控件时,代码会打印一张空白的纸。为什么会这样?我的代码如下 private void PrintCurrentTab(object sender, RoutedEventArgs e) { PrintDialog printDlg = new PrintDialog(); var child = MyMainWindowViewModel.SelectedTab.Content; if

我有一个UserControl,我在代码隐藏中实例化它,并希望打印它。当我打印这个用户控件时,代码会打印一张空白的纸。为什么会这样?我的代码如下

private void PrintCurrentTab(object sender, RoutedEventArgs e)
{
    PrintDialog printDlg = new PrintDialog();
    var child = MyMainWindowViewModel.SelectedTab.Content;
    if (child is ScrollViewer)
    {
        child = (((ScrollViewer)child).Content);
    }

    if (printDlg.ShowDialog() == true)
    {
        var printControl = new PrintingTemplate();
        printDlg.PrintVisual(printControl, "User Control Printing.");
    }
}
<UserControl x:Class="MyApp.Views.PrintingTemplate"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         MinHeight="500"
         MaxHeight="1000"
         MinWidth="200"
         MaxWidth="1000"
         Height="1056"
         Width="816">
    <StackPanel>
        <Grid>
            <Image Source="..\Resources\Images\PrintLogo.jpg" Width="150" HorizontalAlignment="Right" Margin="20"/>
            <Rectangle Fill="Black" Margin="10,40,150,0" Height="2"/>
        </Grid>
        <Grid Name="PrintingGrid"/>
        <StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
            <Label Content="Printed By:"/>
            <Label Name="PrintedBy"/>
            <Label Content="Printed On:"/>
            <Label Name="PrintedDate"/>
        </StackPanel>            
    </StackPanel>
</UserControl>
我的UserControl如下所示

private void PrintCurrentTab(object sender, RoutedEventArgs e)
{
    PrintDialog printDlg = new PrintDialog();
    var child = MyMainWindowViewModel.SelectedTab.Content;
    if (child is ScrollViewer)
    {
        child = (((ScrollViewer)child).Content);
    }

    if (printDlg.ShowDialog() == true)
    {
        var printControl = new PrintingTemplate();
        printDlg.PrintVisual(printControl, "User Control Printing.");
    }
}
<UserControl x:Class="MyApp.Views.PrintingTemplate"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         MinHeight="500"
         MaxHeight="1000"
         MinWidth="200"
         MaxWidth="1000"
         Height="1056"
         Width="816">
    <StackPanel>
        <Grid>
            <Image Source="..\Resources\Images\PrintLogo.jpg" Width="150" HorizontalAlignment="Right" Margin="20"/>
            <Rectangle Fill="Black" Margin="10,40,150,0" Height="2"/>
        </Grid>
        <Grid Name="PrintingGrid"/>
        <StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
            <Label Content="Printed By:"/>
            <Label Name="PrintedBy"/>
            <Label Content="Printed On:"/>
            <Label Name="PrintedDate"/>
        </StackPanel>            
    </StackPanel>
</UserControl>

您调试过这个吗?你的问题似乎不完整,我真的不知道你想要实现什么。我确实注意到缺少一个重要的步骤。以下是我的一些问题:

  • 是否
    MyMainWindowViewModel.SelectedTab.Content==null
  • 孩子的类型是什么
  • child=((ScrollViewer)child.Content)的用途是什么--太难看了
  • 实际上,为什么任何
    代码都在那里,它没有被使用
    无论如何,不能打印未通过布局过程的控件。所以现在您可能会问,“如何强制呈现控件?”很简单,如下所示:

    if (printDlg.ShowDialog() == true)
    {
        var printableArea = new Size(printDlg.PrintableAreaWidth, printDlg.PrintableAreaHeight)
    
        var printControl = new PrintingTemplate();
    
        //Set the drawing dimensions/boundaries - notice (Acutal)Width/Height = 0
        printControl.Measure(printableArea);
        printControl.Arrange(new Rect(new Point(), printableArea);
    
        //"Render"!
        printcontrol.UpdateLayout();
        //At this point you should see the (Acutal)Width/Height be > 0!      
    
        printDlg.PrintVisual(printControl, "User Control Printing.");
    }
    

    我遇到了一个类似的问题,我可以在一些计算机上打印,但不能在物理打印机上打印(只有空白页)(可以使用XPS)。我终于在这里找到了一个可行的解决方案:

    »

    问候,

    魁北克省的弗朗索瓦

    也许以下代码中的新的就是罪魁祸首

     if (printDlg.ShowDialog() == true)
        {
            var printControl = new PrintingTemplate();
            printDlg.PrintVisual(printControl, "User Control Printing.");
        }
    

    尝试获取当前控件的句柄,而不是创建一个新的

    为什么snark?这个问题很好,可以开始讨论了。您提供的代码仍然打印空白页。我发现的唯一解决方案是,让用户控制一个窗口,以最小化的状态显示窗口,打印出来,然后关闭它。