gsharp
gsharp

Reputation: 27937

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

Upvotes: 11

Views: 14023

Answers (2)

Bill Sithiro
Bill Sithiro

Reputation: 21

In case you're looking to do the same thing in code for the Width property of a Column of a normal Grid control, use GridLength.Auto.

Upvotes: 2

Matt Hamilton
Matt Hamilton

Reputation: 204269

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.

Upvotes: 15

Related Questions