Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

ColumnDefinition.Width Property

Gets the calculated width of a ColumnDefinition element, or sets the GridLength value of a column that is defined by the ColumnDefinition.

Namespace: System.Windows.Controls
Assembly: PresentationFramework (in presentationframework.dll)
XML Namespace:  http://schemas.microsoft.com/winfx/2006/xaml/presentation

public GridLength Width { get; set; }
/** @property */
public GridLength get_Width ()

/** @property */
public void set_Width (GridLength value)

public function get Width () : GridLength

public function set Width (value : GridLength)

<object Width="GridLength" .../>

Property Value

The GridLength that represents the width of the Column. The default value is 1.0.

Identifier Field

WidthProperty

Metadata Flags

None

The default GridUnitType is Star.

When you add or remove rows or columns, the ActualWidth for all ColumnDefinition elements and the ActualHeight of all RowDefinition elements becomes zero until Measure is called.

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.

To view the complete sample, including the related XAML file, see Instantiating and Using a GridLengthConverter Object Sample.

public void changeColVal(object sender, RoutedEventArgs e) 
{
    txt1.Text = "Current Grid Column is " + hs1.Value.ToString();
}

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

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft