Search code examples
wpfdata-binding.net-3.5printingflowdocument

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.


Solution

  • 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.