Question

I made a control which shows some data within a DataGrid.
When used within a window, it works without issue.
When i write it to an Xps file, the header gets shifted and the output is wrong. To write it to an XPS, i create my control, then i fill in the data it needs to display.

I used this post : Convert WPF (XAML) Control to XPS Document to output the control to an Xps file.

I tried to have the HeaderRowTemplate to a fixed size with no result.

I also tried to update the layout after the data update with no result.

The Columns and rows are computed on-the-fly, depending on which data is non-null.

(Rq : vertical and horizontal properties are not the same)

Here's how looks the control with some data loaded within a window :

Right display, inside a window

And there's a screenshot of the xps file generated from this control.

ScreenShot of the xps generated

Edit : i finally found a workaround for this issue : i added a 'Print Preview' window in which the controls are shown -which user like anyway- and i 'print' this (correct) window content.

Was it helpful?

Solution

I had the same problem with DataGrid and identified the dynamic width (*) of DataGridColumn to be the root of evil. In live application everything seems fine, but after converting to XPS, the width is ignored. My overall width of the DataGrid is fixed, so for me, setting a fixed Width new DataGridLength(80.0) for example did the trick. By default it is a star Width (1*). To be able to define relational columns I wrote a MarkupExtension, which gets the available width, the number of columns and the 'star amount':

<DataGridColumn Width="{local:MyWidth Amount=0.5, Columns=5, TotalWidth=800}" />

The calculation for this example wound be: TotalWidth / Columns * Amount. This works great in XPS print. You could also calculate the total width and colum count with some tree climbing and resolve it directly in the extension (ActualWidth and Columns.Count).

Edit: this is a possible implementation for the MarkupExtension

[MarkupExtensionReturnType(typeof(double))]
public class MyWidth : MarkupExtension
{
    #region Constructors (2) 

    public MyWidth(double amount, int columns, double totalWidth)
    {
        this.Amount = amount;
        this.Columns = columns;
        this.TotalWidth = totalWidth;
    }

    public MyWidth() { }

    #endregion Constructors 

    #region Properties (3) 

    [ConstructorArgument("amount")]
    public double Amount { get; set; }

    [ConstructorArgument("columns")]
    public int Columns { get; set; }

    [ConstructorArgument("totalWidth")]
    public double TotalWidth { get; set; }

    #endregion Properties 

    #region Methods  (1) 

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        return this.TotalWidth / (double)this.Columns * this.Amount;
    }

    #endregion Methods  
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top