Skip to main content
.NET Framework Class Library
GridLength Structure

Represents the length of elements that explicitly support Star unit types.

Namespace:   System.Windows
Assembly:  PresentationFramework (in PresentationFramework.dll)
XMLNS for XAML: http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation
Syntax
<[%$TOPIC/ms602745_en-us_VS_110_2_0_0_0_0%](GetType([%$TOPIC/ms602745_en-us_VS_110_2_0_0_0_1%]))> _
Public Structure GridLength _
	Implements [%$TOPIC/ms602745_en-us_VS_110_2_0_0_0_2%](Of GridLength)
[[%$TOPIC/ms602745_en-us_VS_110_2_0_1_0_0%](typeof([%$TOPIC/ms602745_en-us_VS_110_2_0_1_0_1%]))]
public struct GridLength : [%$TOPIC/ms602745_en-us_VS_110_2_0_1_0_2%]<GridLength>
[[%$TOPIC/ms602745_en-us_VS_110_2_0_2_0_0%](typeof([%$TOPIC/ms602745_en-us_VS_110_2_0_2_0_1%]))]
public value class GridLength : [%$TOPIC/ms602745_en-us_VS_110_2_0_2_0_2%]<GridLength>
[<[%$TOPIC/ms602745_en-us_VS_110_2_0_3_0_0%]>]
[<[%$TOPIC/ms602745_en-us_VS_110_2_0_3_0_1%](typeof([%$TOPIC/ms602745_en-us_VS_110_2_0_3_0_2%]))>]
type GridLength =  
    struct 
        interface [%$TOPIC/ms602745_en-us_VS_110_2_0_3_0_3%]<GridLength>
    end
<object property="doubleLength"/>
- or -
<object property="autoValue"/>

XAML Values

doubleLength

The element’s length, described as either a Double value that specifies a device-independent units (1/96th inch per unit) measure, or as a qualifiedDouble (see below).

autoValue

The element's length, described by either the value Auto or *. See GridUnitType for more information on the values Auto and *.

qualifiedDouble

A double value as described above for any of the measures, followed by one of the following unit declaration strings: px, in, cm, pt.

px (default) is device-independent units (1/96th inch per unit)

in is inches; 1in==96px

cm is centimeters; 1cm==(96/2.54) px

pt is points; 1pt==(96/72) px

The GridLength type exposes the following members.

Constructors
  NameDescription
Public method GridLength(Double)Initializes a new instance of the GridLength structure using the specified absolute value in pixels.
Public method GridLength(Double, GridUnitType)Initializes a new instance of the GridLength structure and specifies what kind of value it holds.
Top
Properties
  NameDescription
Public property Static member AutoGets an instance of GridLength that holds a value whose size is determined by the size properties of the content object.
Public property GridUnitTypeGets the associated GridUnitType for the GridLength.
Public property IsAbsoluteGets a value that indicates whether the GridLength holds a value that is expressed in pixels.
Public property IsAutoGets a value that indicates whether the GridLength holds a value whose size is determined by the size properties of the content object.
Public property IsStarGets a value that indicates whether the GridLength holds a value that is expressed as a weighted proportion of available space.
Public property ValueGets a Double that represents the value of the GridLength.
Top
Methods
  NameDescription
Public method Equals(GridLength)Determines whether the specified GridLength is equal to the current GridLength.
Public method Equals(Object)Determines whether the specified object is equal to the current GridLength instance. (Overrides ValueTypeEquals(Object).)
Public method GetHashCodeGets a hash code for the GridLength. (Overrides ValueTypeGetHashCode.)
Public method GetTypeGets the Type of the current instance. (Inherited from Object.)
Public method ToStringReturns a String representation of the GridLength. (Overrides ValueTypeToString.)
Top
Operators
  NameDescription
Public operator Static member EqualityCompares two GridLength structures for equality.
Public operator Static member InequalityCompares two GridLength structures to determine if they are not equal.
Top
Remarks

Elements such as ColumnDefinition and RowDefinition use this type to describe width and height in order to support variable distribution of available space.

Examples

The following example shows how to create and use an instance of GridLengthConverter. The example defines a custom method called changeCol, which passes the ListBoxItem to a GridLengthConverter that converts the Content of a ListBoxItem to an instance of GridLength. The converted value is then passed back as the value of the Width property of the ColumnDefinition element.

The example also defines a second custom method, called changeColVal. This custom method converts the Value of a Slider to a String and then passes that value back to the ColumnDefinition as the Width of the element.

Note that a separate Extensible Application Markup Language (XAML) file defines the contents of a ListBoxItem.

Private Sub changeColVal(ByVal sender As Object, ByVal args As RoutedPropertyChangedEventArgs(Of Double))

    txt1.Text = "Current Grid Column is " + hs1.Value.ToString()
End Sub 

Private Sub changeCol(ByVal sender As Object, ByVal args As SelectionChangedEventArgs)

    Dim li1 As ListBoxItem = CType(CType(sender, ListBox).SelectedItem, ListBoxItem)
    Dim myGridLengthConverter As System.Windows.GridLengthConverter = New System.Windows.GridLengthConverter()
    If (hs1.Value = 0) Then 

        Dim gl1 As GridLength = CType(myGridLengthConverter.ConvertFromString(li1.Content.ToString()), GridLength)
        col1.Width = gl1

    ElseIf (hs1.Value = 1) Then 

        Dim gl2 As GridLength = CType(myGridLengthConverter.ConvertFromString(li1.Content.ToString()), GridLength)
        col2.Width = gl2

    ElseIf (hs1.Value = 2) Then 

        Dim gl3 As GridLength = CType(myGridLengthConverter.ConvertFromString(li1.Content.ToString()), GridLength)
        col3.Width = gl3
    End If 
End Sub
private void changeColVal(object sender, RoutedEventArgs e) 
		{
            txt1.Text = "Current Grid Column is " + hs1.Value.ToString();
        }

        private void changeCol(object sender, SelectionChangedEventArgs args)
        {
            ListBoxItem li = ((sender as ListBox).SelectedItem as ListBoxItem);
            GridLengthConverter myGridLengthConverter = new GridLengthConverter();
            if (hs1.Value == 0)
            {
                GridLength gl1 = (GridLength)myGridLengthConverter.ConvertFromString(li.Content.ToString());
                col1.Width = gl1;
            }
            else if (hs1.Value == 1)
            {
                GridLength gl2 = (GridLength)myGridLengthConverter.ConvertFromString(li.Content.ToString());
                col2.Width = gl2;
            }
            else if (hs1.Value == 2)
            {
                GridLength gl3 = (GridLength)myGridLengthConverter.ConvertFromString(li.Content.ToString());
                col3.Width = gl3;
            }
        }

More Code

How to: Build a Standard UI Dialog Box by Using GridThis example shows how to create a standard user interface (UI) dialog box by using the Grid element.
Version Information

.NET Framework

Supported in: 4.5.3, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Platforms

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Thread Safety
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.