Musaab
Musaab

Reputation: 805

Databinding lost when printing data-bound FlowDocument

I'm losing data-bind when initiating a printing process, is that possible ? Thats what I can only think of in my situation here, where I have a Table inside a control that makes the Table data-bindable, all inside a FlowDocument. When running it the data-bind works fine and the table draws itself with some data on it with no problems.

However, when printing the output of that control is always blank.

I've added a ListView with the same bindings and when printing the generated data it too appears lost.

XAML:

<Window x:Class="GlassStore.InitBill"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:GlassStore.ViewModels"
        xmlns:flowdocs="clr-namespace:FlowDocuments;assembly=FlowDocument"
        Title="InitBill" Height="825" Width="1004">
<Window.DataContext>
    <local:InitBillViewModel/>
</Window.DataContext>
<Grid Background="White">
    <FlowDocumentReader HorizontalAlignment="Center"
                        HorizontalContentAlignment="Center">
        <FlowDocument ColumnWidth="999999"
                      IsColumnWidthFlexible="True"
                      TextAlignment="Center" 
                      Name="FD">
                <Paragraph>
                <ListView ItemsSource="{Binding GridTrans}">
                    <ListView.View>
                        <GridView>
                            <GridViewColumn Header="ffff"
                                 DisplayMemberBinding="{Binding CarModel}" />
                            <GridViewColumn Header="xxxx"
                                 DisplayMemberBinding="{Binding CarName}" />
                        </GridView>
                    </ListView.View>
                </ListView>
            </Paragraph>
            <Paragraph TextAlignment="Center">
                <TextBlock Text="{Binding test}" />
            </Paragraph>
            <flowdocs:ItemsContent ItemsSource="{Binding GridTrans}"
                                   Background="#FFF2C3C3"
                                   BorderThickness="2">
                <flowdocs:ItemsContent.ItemTemplate>
                    <DataTemplate>
                        <flowdocs:Fragment>
                            <Table>
                            <TableRowGroup flowdocs:Attached.IsItemsHost="True">
                                <TableRow Background="AliceBlue" >
                                    <TableCell Foreground="Red">
                                        <Paragraph>
                                            <flowdocs:BindableRun BoundText="{Binding CarName}" />
                                        </Paragraph>
                                    </TableCell>
                                    <TableCell Foreground="Green">
                                        <Paragraph>
                                            <flowdocs:BindableRun BoundText="{Binding CarModel}" />
                                        </Paragraph>
                                    </TableCell>
                                    <TableCell Foreground="Yellow">
                                        <Paragraph>
                                            <flowdocs:BindableRun BoundText="{Binding glassPrice}" />
                                        </Paragraph>
                                    </TableCell>
                                </TableRow>
                            </TableRowGroup>
                            </Table>
                        </flowdocs:Fragment>
                    </DataTemplate>
                </flowdocs:ItemsContent.ItemTemplate>
            </flowdocs:ItemsContent>
            <Table>
                <TableRowGroup>
                    <TableRow>
                        <TableCell>
                            <Paragraph>Row1 Cell1</Paragraph>
                        </TableCell>
                        <TableCell>
                            <Paragraph>Row2 Cell2</Paragraph>
                        </TableCell>
                    </TableRow>
                </TableRowGroup>
            </Table>  
        </FlowDocument>
    </FlowDocumentReader>
    <Button Command="{Binding print}"
            Content="إطـبع"
            Height="29" Margin="91,0,112,41"
            Name="button1"
            VerticalAlignment="Bottom" />
</Grid>
</Window>

Now I know the problem is not with the Custom Control, because I have the same problem now with ListView.

I've attached the source to the Window version here and the printed version here.

Upvotes: 1

Views: 2360

Answers (2)

pistipanko
pistipanko

Reputation: 775

Here's my solution:

  1. define the document in a separate XAML file:

Load the Flow Document from Resource Dictionary Xaml

  1. then print:

        var prntDlg = new PrintDialog();
        var res = Application.LoadComponent(new Uri("/Resources/ReportDocument.xaml", UriKind.RelativeOrAbsolute)) as ResourceDictionary;
        var doc = res["ReportFlowDoc"] as FlowDocument;
    
        doc.DataContext = this.fdswReport.Document.DataContext; //your FlowDocumentScrollViewer
        doc.PageWidth = prntDlg.PrintableAreaWidth;
        doc.PageHeight = prntDlg.PrintableAreaHeight;
        doc.ColumnWidth = prntDlg.PrintableAreaWidth;
        doc.PagePadding = new Thickness(80);
        doc.IsOptimalParagraphEnabled = true;
        doc.IsHyphenationEnabled = true;
    
        if (prntDlg.ShowDialog() == true)
            prntDlg.PrintDocument(((IDocumentPaginatorSource)doc).DocumentPaginator, "Report");
    

Upvotes: 0

J&#246;rg Reichardt
J&#246;rg Reichardt

Reputation: 361

The ViewModel would be nice, especially the method behind the print command. My guess ist, that the flowdocument is put into a special print context and loses the datacontext of the window.

Try removing

<Window.DataContext>
    <local:InitBillViewModel/>
</Window.DataContext>

and use

<FlowDocumentReader HorizontalAlignment="Center" HorizontalContentAlignment="Center">
    <FlowDocumentReader.DataContext>
        <local:InitBillViewModel/>
    </FlowDocumentReader.DataContext>
...

instead. Maybe that helps?

Edit: The print command would have to move to another ViewModel to still work, of course. This other ViewModel would stay where the old one was, in the Window.DataContext.

Upvotes: 3

Related Questions