Autosize GridViewColumns programmatically by content
-
25-05-2021 - |
Question
I want to set programmatically my GridViewColumns to autosize by content, not by header (Width = double.NaN)
I have searched for a long time and I found this problem solved with DataGridColumns, but how is this with GridViewColumns ?
Solution
I had the same problem, found a good hint on it here.
This is how I solved the problem.
if ((sender as ListView)?.View is GridView gridview)
{
foreach (var column in gridview.Columns)
{
// Set the Width. Then clear it to cause the autosize.
column.Width = 1;
column.ClearValue(GridViewColumn.WidthProperty);
}
}
OTHER TIPS
Try this:
foreach (DataGridColumn column in grid.Columns) {
column.Width = DataGridLength.SizeToCells;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow