【问题标题】:Databinding lost when printing data-bound FlowDocument打印数据绑定 FlowDocument 时数据绑定丢失
【发布时间】:2011-09-25 04:05:33
【问题描述】:

我在启动打印过程时丢失了数据绑定,这可能吗?这就是我在我这里的情况下只能想到的,我在一个控件中有一个 Table,它使 Table 数据可绑定,所有这些都在 FlowDocument 内。运行它时,数据绑定工作正常,表格自己绘制了一些数据,没有问题。

但是,当打印该控件的输出时总是空白。

我添加了具有相同绑定的ListView,并且在打印生成的数据时它也似乎丢失了。

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>

现在我知道问题不在于自定义控件,因为我现在与 ListView 有同样的问题。

我已将源附加到 Window 版本 here 和印刷版本 here。

【问题讨论】:

  • 一些代码会很好。
  • 我和@Femaref 在一起,我们需要查看一些 XAML 以及可能与它相关的任何代码。
  • 添加了 XAML ,我没有代码隐藏,因为我遵循 MVVM 模式,我有一个 viewModel 代替,先看图片,如果需要 viewModel ,我将添加它也是
  • 我们能看到打印命令的代码吗?

标签: wpf data-binding .net-3.5 printing flowdocument


【解决方案1】:

ViewModel 会很好,尤其是 print 命令背后的方法。我的猜测是,流文档被放入一个特殊的打印上下文并丢失了窗口的数据上下文。

尝试删除

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

并使用

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

相反。也许这有帮助?

编辑:当然,打印命令必须移动到另一个 ViewModel 才能继续工作。另一个 ViewModel 将保留在旧 ViewModel 的位置,在 Window.DataContext 中。

【讨论】:

  • 不敢相信这真的解决了,现在的生活很好,非常感谢你们所有人,特别是 Jörg Reichardt 先生
【解决方案2】:

这是我的解决方案:

  1. 在单独的 XAML 文件中定义文档:

Load the Flow Document from Resource Dictionary Xaml

  1. 然后打印:

        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");
    

【讨论】:

    猜你喜欢
    • 2010-10-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-27
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 2010-10-24
    • 1970-01-01
    相关资源
    最近更新 更多