Search code examples
c#wpflistviewgridviewwpf-controls

HowTo define the "Auto" Width of the WPF GridView Column in Code?


I want to define the "Auto" width of a GridView Column in the code. How can I do that?

var grid = (GridView)myListview.View;
grid.Columns.Add(new GridViewColumn
{
   Header = "My Header",
   DisplayMemberBinding = new Binding("MyBinding"),
   Width = ??? // Auto
});

Solution

  • GridViewColumn's Width property is of type double, but according to the MSDN page you can set it to Double.NaN ("not a number") to tell it to auto-size.

    If you do that, you have to ask for its ActualWidth if you want to know the width it has auto-sized to.