I'm running into issues with column widths in two different areas using the 11.1 release. I have a grid where all columns have Width="*" in their XAML.
First, when we try to export the grid to Excel, all columns are very small. See below:
I also have a customized GroupByRecordPresenter. A grid column is bound to the width of the first xamdatagrid column. Here's the xaml:
<Grid.ColumnDefinitions> <ColumnDefinition Width="{Binding ElementName=grdOrder, Path=DefaultFieldLayout.Fields[0].CellWidthResolved}"/> <ColumnDefinition Width="Auto"/> <ColumnDefinition Width="Auto"/> <ColumnDefinition Width="Auto"/> <ColumnDefinition Width="Auto"/> <ColumnDefinition Width="Auto"/> </Grid.ColumnDefinitions> <TextBlock Background="White" Foreground="Black" Text="{Binding Path=Record.ChildRecords, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource ATMTextConverter}, ConverterParameter=Instrument}" Margin="0,0,1,0" Grid.Column="0"/>
The result of this binding is below:
You can see some text on the left cut off.
For both the Excel export and the grouping customization, manually changing the column width fixes the layout problems. For example:
In both cases, I just want the column widths to just work when the xamdatagrid column width is set to star. I have been unable to get a usable value from CellWidthResolved or LabelWidthResolved properties.
Hell Dierk,
I have been looking into your first issue and I could not be able to reproduce it on my side. I am attaching a sample application(XamDataGridExportToExcel.zip) that I have used for my tests.
Could you please modify it in order to meet exactly your scenario ?
Looking forward to hearing from you.
Hi Dierk,
I believe the reason for this behavior is that your DataGrid has too many columns.
May be you can add some additional logic to the export process. For example you can hook up to the HeaderAreaExporting event...
DataPresenterExcelExporter exporter = new DataPresenterExcelExporter();
exporter.HeaderAreaExporting += new EventHandler<HeaderAreaExportingEventArgs>(exporter_HeaderAreaExporting);
... and then add some additional formatting on Workbook object that is being created, using the event arguments
void exporter_HeaderAreaExporting(object sender, HeaderAreaExportingEventArgs e)
{
foreach (var field in e.FieldLayout.Fields)
e.Workbook.Worksheets[0].Columns[field.Index].SetWidth(field.LabelWidthResolved, WorksheetColumnWidthUnit.Point);
}
If you need more help, you can write back or if possible attach a sample, so that I can eventually get deeper,
Thanks,
Stefana
Stefana,
How many columns is too many? Also, is the SetWidth method available in the 11.1 release? I get a compile error when using that method.
Also remember that the Excel export is not the only place I see this behavior. As my original post states, I also see this problem when I try binding to CellWidthResolved or LabelWidthResolved on other UI elements. Please see the screenshots/code in my original post.
To provide more information, we are using a custom class that inherits from XamDataGrid. This is primarily so we can add custom context menus to all instances where we use the grid. We handle OnFieldLayoutInititialized events with custom logic.
Yanko,
I am so far unable to replicate the problem with the sample you provided. I will keep looking into it.
Would you please try to provide a sample, so that I can try to assist you further.
Thanks!
Quick update: I discovered that I had not updated to the latest 11.1 service release from May of last year. This fixed the Excel column width export bug. However, the issue with binding to the LabelWidthResolved field of a column whose width is set to star is still unresolved. I have also been unable to replicate this in a standalone test project.
Hello Dierk,
Since the Star Width is resolved after the ColumnDefinition is created and LabelWidthResolved is set it is expected that the Binding doesn't work. Also the LabelWidthResolved doesn't have a PropertyChanged notifier, so I created a sample project where I implemented the functionality you want. Basically I created a Style for the LabelPresenter and handled its Loaded event. In the handler I bound the ColumnDefinitio's Width to the LabelPresenter's ActualWidth Property, which has a PropertyChanged notifier.
Please let me know if this helps you or you need further assistance on this matter.
Looking forward for your reply.
Thank you for your feedback. I am glad that you resolved your issue and I believe that other community members may benefit from this as well.
Thanks again.
Thanks Stefan, this pointed me in the direction I needed to go and solved my issue.