打印数据绑定 FlowDocument 时数据绑定丢失
我在启动打印过程时丢失数据绑定,这可能吗?这就是我在我的情况下只能想到的,我在控件内有一个表,使表数据可绑定,所有这些都在 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
也有同样的问题。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
ViewModel 会很好,尤其是打印命令背后的方法。我的猜测是,流程文档被放入特殊的打印上下文中,并丢失了窗口的数据上下文。
尝试删除
并使用
。也许这有帮助?
编辑:当然,打印命令必须移动到另一个 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
and use
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.
这是我的解决方案:
从资源字典Xaml加载流程文档
然后打印:
Here's my solution:
Load the Flow Document from Resource Dictionary Xaml
then print: