C# WPF - GridLength GridUnitType.Auto

18.2k views Asked by At

Can anyone explain the difference between using:

 GridLength length = new GridLength(0, GridUnitType.Auto) 

and

 GridLength length = new GridLength(1, GridUnitType.Auto)

My limited knowledge of this leads me to believing these would both be identical solutions due to auto being as it states..."auto", therefore making the double value redundant.

Most examples I have seen show the GridUnitType.Auto being preceded with 1 rather than 0, but it seems to me that either option works the same?

Is this the case or can anyone shed some light on if/how these are different

1

There are 1 answers

3
Chris On BEST ANSWER

I think your understanding is correct, when the value GridUnitType.Auto is used, the first value passed to the constructor is redundant, as the size will be determined by the content object.

It somewhat makes sense in the context of the GridLength structure constructor to retain this parameter (even though it's not used in this instance), as it allows the second parameter type to contain values that describe the all available states of GridUnitType.

From the documentation:

The enumerated type GridUnitType can contain the following values:

Auto   - The size is determined by the size properties of the content object. 
Pixel  - The value is expressed as a pixel. 
Star   - The value is expressed as a weighted proportion of available space. 

So really, the first parameter is only relevant when the second parameter is set to GridUnitType.Pixel or GridUnitType.Star.

It wouldn't work neatly other way around e.g. if you tried to have constructor that accepted 1 parameter as a GridUnitType, and only required the second parameter if you used Pixel or Star.

This way round, you get the benefit of having a 1 parameter constructor that accepts a double without specifying the additional type. Although it does have the cost of a potentially odd looking two parameter constructor when using Auto (as in your example).