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
}