4

I have an application that fetch data from sensor and then outputs Graphs and Datagrid from that data.

What worked : I was generating a report with usercontrol that were inserted in a fixedPage that were shown in a FixedDocument with the help of a DocumentViewer.

What I then did : In order to facilitate many things, enabling text search, pre-generating the report, ..., I decided to convert my FixedDocument in a XpsDocument and putting this in the DocumentViewer.

My Problem : For some reason, everything was unchanged by the conversion except my DataGrid columns. I am manualy inserting my column in Xaml and each have a Width of 1*. But the result I am getting is as if it were completely ignoring the resize. It's not even resizing properly to the content.

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>
4

0 回答 0