我在启动打印过程时丢失了数据绑定,这可能吗?多数民众赞成在我的情况下我只能想到这一点,我在一个控件中有一个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
相同的问题。
答案 0 :(得分:3)
ViewModel会很好,尤其是print命令背后的方法。我的猜测是,将flowdocument放入特殊的打印上下文并丢失窗口的datacontext。
尝试删除
<Window.DataContext>
<local:InitBillViewModel/>
</Window.DataContext>
并使用
<FlowDocumentReader HorizontalAlignment="Center" HorizontalContentAlignment="Center">
<FlowDocumentReader.DataContext>
<local:InitBillViewModel/>
</FlowDocumentReader.DataContext>
...
代替。也许这有帮助吗?
编辑:当然,打印命令必须移动到另一个ViewModel才能继续工作。在Window.DataContext中,这个其他ViewModel将保留原来的那个。
答案 1 :(得分:0)
这是我的解决方案:
Load the Flow Document from Resource Dictionary Xaml
然后打印:
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");