Search code examples
c#wpfgridviewcolumn

Autosize GridViewColumns programmatically by content


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);
        }
    }