打印数据绑定 FlowDocument 时数据绑定丢失

发布于 2024-11-18 02:14:49 字数 5022 浏览 3 评论 0 原文

我在启动打印过程时丢失数据绑定,这可能吗?这就是我在我的情况下只能想到的,我在控件内有一个表,使表数据可绑定,所有这些都在 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 版本 此处 和打印版本这里

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

表情可笑 2024-11-25 02:14:49

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

尝试删除

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

并使用

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

。也许这有帮助?

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

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.

将军与妓 2024-11-25 02:14:49

这是我的解决方案:

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

从资源字典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; //你的FlowDocumentScrollViewer
        doc.PageWidth = prntDlg.PrintableAreaWidth;
        doc.PageHeight = prntDlg.PrintableAreaHeight;
        doc.ColumnWidth = prntDlg.PrintableAreaWidth;
        doc.PagePadding = 新厚度(80);
        doc.IsOptimalParagraphEnabled = true;
        doc.IsHyphenationEnabled = true;
    
        if (prntDlg.ShowDialog() == true)
            prntDlg.PrintDocument(((IDocumentPaginatorSource)doc).DocumentPaginator, "报告");
    

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");
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文