Calculate/Get Column ActualWidth for virtualized columns

1 Answer 43 Views
GridView
Marco
Top achievements
Rank 1
Marco asked on 23 Oct 2021, 01:21 AM | edited on 23 Oct 2021, 01:21 AM

Hello,

i implemented a feature "auto fit columns" which just set every column to with = auto:

foreach (var column in ItemGrid.Columns)
{
	column.Width = GridViewLength.Auto;
}

After this i will save the ActualWidth of all Columns. This works for all rendered columns, but not for the virtualized (they are always 20 pixels).

I tried a lot workarounds, but nothing works or causes UI glitches (Disable and Enable virtualization):

ItemGrid.EnableColumnVirtualization = false;                            

foreach (var column in ItemGrid.Columns)
{
	column.Width = GridViewLength.Auto;
	ItemGrid.ScrollIntoView(null, column);
	ItemGrid.UpdateLayout();
	column.Width = column.ActualWidth;
	column.IsSelected = true;
	column.Focus();
	column.Width = column.ActualWidth;
}

ItemGrid.UpdateLayout();

SaveLayout();

ItemGrid.EnableColumnVirtualization = true;

I think I need something like the "GridColumnSizer" from Syncfunsion (https://help.syncfusion.com/wpf/datagrid/autosize-columns#customizing-built-in-column-auto-sizing-logic) to calculate the width.

Do you have any ideas how to solve this problem?

regards, Marco

1 Answer, 1 is accepted

Sort by
0
Dilyan Traykov
Telerik team
answered on 27 Oct 2021, 02:31 PM

Hello Marco,

Thank you for the provided code snippets.

Based on them, I set up a small sample project where using the approach you shared seems to work as expected - the columnWidths List is populated with the correct values.

Can you please have a look and let me know if I'm missing something of importance? If possible, please modify the project and send it back to me so that I can further investigate.

Regards,
Dilyan Traykov
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Marco
Top achievements
Rank 1
commented on 29 Oct 2021, 03:19 PM

Thank you for your efforts.

Your project works for me. On my side i got a lot of empty grids (trigger). When i try to hack around with the "ScrollIntoView" Method nothing happens. (See attached Example)

Furthermore "ScrollIntoView" isn't the cleanest Solution.

regards
Marco

Dilyan Traykov
Telerik team
commented on 03 Nov 2021, 12:31 PM

Hello Marco,

Thank you for the updated project.

Indeed, the ScrollIntoView method will not work in this scenario if the control does not have any items.

As I'm unable to think of another suitable solution to correctly measure all the header cells, however, at this point I can only suggest inserting a dummy item so that the measurements can be made and then removing it from the ItemsSource.

I've updated the sample project to demonstrate how this would work. I do hope you find this approach applicable.
Marco
Top achievements
Rank 1
commented on 05 Nov 2021, 03:54 PM

Hi Dilyan,

thank you again. I tried your approach, but it wont work for the most cases, because my viewModels have mostly no parameterless constructor. If I had to add a parameterless constructor to every viewModel my generic approach gets lost and there are a lot of error potential. So for now I stick with "half" solution without a dummy object.

If there are other ideas, I am open to it ;)

Tags
GridView
Asked by
Marco
Top achievements
Rank 1
Answers by
Dilyan Traykov
Telerik team
Share this question
or