Automate GridViewColumns programmatically by content

I want to programmatically set my GridViewColumns to auto-negotiate by content, not title (Width = double.NaN)

I searched for a long time and solved this problem with DataGridColumns, but how does this happen with GridViewColumns?


source to share

2 answers

I had the same problem, I found a good hint of 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;




Try the following:

foreach (DataGridColumn column in grid.Columns) {
      column.Width = DataGridLength.SizeToCells;




All Articles