I have an application that retrieves data from a sensor and then prints graphics and Datagrid from that data.
What worked : I created a report with usercontrol that was inserted into a fixedPage, which was displayed in a FixedDocument using a DocumentViewer.
What I did then : To facilitate a lot by enabling text search, having previously created the report ..., I decided to convert my FixedDocument to XpsDocument and put it in the DocumentViewer.
My problem : For some reason, everything changed without converting the columns of the DataGrid. I can insert my column in Xaml, and each one has a width of 1 *. But the result I get is as if it completely ignores resizing. It doesn't even resize the content correctly.
What fixes it . When I put a fixed width, it is respected in the viewer.
Code :
var actualReport = new FixedDocument(); actualReport.DocumentPaginator.PageSize = new Size(8.5 * 96, 11 * 96); actualReport.Pages.Add(new PageContent { Child = new FixedPage { Children = { new FrontPage { Width = 8.5 * 96, Height = 11 * 96, DataContext = d, } } } }); [.. Three more pages ..] var ms = new MemoryStream(); var pkg = Package.Open(ms, FileMode.Create, FileAccess.ReadWrite); string pack = "pack://temp.xps"; PackageStore.AddPackage(new Uri(pack), pkg); var doc = new XpsDocument(pkg, CompressionOption.SuperFast, pack); XpsSerializationManager rsm = new XpsSerializationManager(new XpsPackagingPolicy(doc), false); rsm.SaveAsXaml(actualReport.DocumentPaginator); Viewer.Document = doc.GetFixedDocumentSequence();
Style :
<Style TargetType="DataGrid" x:Key="PrintGrid"> <Setter Property="IsReadOnly" Value="True" /> <Setter Property="BorderBrush" Value="{x:Null}" /> <Setter Property="Background" Value="{x:Null}" /> <Setter Property="AutoGenerateColumns" Value="False" /> <Setter Property="CanUserSortColumns" Value="False" /> <Setter Property="CanUserResizeColumns" Value="False" /> <Setter Property="CanUserResizeRows" Value="False" /> <Setter Property="CanUserReorderColumns" Value="False" /> <Setter Property="HeadersVisibility" Value="Column" /> <Setter Property="FontSize" Value="16" /> </Style>
Xaml :
<DataGrid Grid.Row="1" ItemsSource="{Binding CollectCtx.Sensors}" Style="{DynamicResource PrintGrid}" HorizontalAlignment="Stretch"> <DataGrid.Columns> <DataGridTextColumn Header="Pos" Width="1*" Binding="{Binding Position}" /> <DataGridTextColumn Header="Min" Width="1*"Binding="{Binding Data.Min, StringFormat={x:Static r:Resources.FormatTemp}}" /> <DataGridTextColumn Header="Ave" Width="1*" Binding="{Binding Data.Ave, StringFormat={x:Static r:Resources.FormatTemp}}" /> <DataGridTextColumn Header="Max" Width="1*" Binding="{Binding Data.Max, StringFormat={x:Static r:Resources.FormatTemp}}" /> <DataGridTextColumn Header="Stab" Width="1*" Binding="{Binding Data.Stab, StringFormat={x:Static r:Resources.FormatTemp}}" /> <DataGridTextColumn Header="Diff" Width="1*" > <DataGridTextColumn.Binding> <MultiBinding StringFormat="{x:Static r:Resources.FormatTemp}" Converter="{StaticResource DiffConverter}"> <Binding Path="Data.Ave" /> <Binding RelativeSource="{RelativeSource AncestorType={x:Type UserControl}}" Path="DataContext.CollectCtx.Temperature" /> </MultiBinding> </DataGridTextColumn.Binding> </DataGridTextColumn> <DataGridTextColumn Header="Compliant" Width="1*" Binding="{Binding DataContext.CollectCtx, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}, Converter={StaticResource CompliantConverter}, Mode=OneWay}" /> </DataGrid.Columns> </DataGrid>