Freigeben über


ColumnDefinition Class

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Defines column-specific properties that apply to Grid objects.

Inheritance Hierarchy

System.Object
  System.Windows.DependencyObject
    System.Windows.Controls.ColumnDefinition

Namespace:  System.Windows.Controls
Assembly:  System.Windows (in System.Windows.dll)

Syntax

'Declaration
Public NotInheritable Class ColumnDefinition _
    Inherits DependencyObject
public sealed class ColumnDefinition : DependencyObject
<ColumnDefinition .../>

The ColumnDefinition type exposes the following members.

Constructors

  Name Description
Public methodSupported by Silverlight for Windows Phone ColumnDefinition Initializes a new instance of the ColumnDefinition class.

Top

Properties

  Name Description
Public propertySupported by Silverlight for Windows Phone ActualWidth Gets a value that represents the actual calculated width of a ColumnDefinition.
Public propertySupported by Silverlight for Windows Phone Dispatcher Gets the Dispatcher this object is associated with. (Inherited from DependencyObject.)
Public propertySupported by Silverlight for Windows Phone MaxWidth Gets or sets a value that represents the maximum width of a ColumnDefinition.
Public propertySupported by Silverlight for Windows Phone MinWidth Gets or sets a value that represents the minimum width of a ColumnDefinition.
Public propertySupported by Silverlight for Windows Phone Width Gets the calculated width of a ColumnDefinition element, or sets the GridLength value of a column that is defined by the ColumnDefinition.

Top

Methods

  Name Description
Public methodSupported by Silverlight for Windows Phone CheckAccess Determines whether the calling thread has access to this object. (Inherited from DependencyObject.)
Public methodSupported by Silverlight for Windows Phone ClearValue Clears the local value of a dependency property. (Inherited from DependencyObject.)
Public methodSupported by Silverlight for Windows Phone Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows Phone Finalize Allows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodSupported by Silverlight for Windows Phone GetAnimationBaseValue Returns any base value established for a Silverlight dependency property, which would apply in cases where an animation is not active. (Inherited from DependencyObject.)
Public methodSupported by Silverlight for Windows Phone GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public methodSupported by Silverlight for Windows Phone GetType Gets the Type of the current instance. (Inherited from Object.)
Public methodSupported by Silverlight for Windows Phone GetValue Returns the current effective value of a dependency property from a DependencyObject. (Inherited from DependencyObject.)
Protected methodSupported by Silverlight for Windows Phone MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported by Silverlight for Windows Phone ReadLocalValue Returns the local value of a dependency property, if a local value is set. (Inherited from DependencyObject.)
Public methodSupported by Silverlight for Windows Phone SetValue Sets the local value of a dependency property on a DependencyObject. (Inherited from DependencyObject.)
Public methodSupported by Silverlight for Windows Phone ToString Returns a string that represents the current object. (Inherited from Object.)

Top

Fields

  Name Description
Public fieldStatic memberSupported by Silverlight for Windows Phone MaxWidthProperty Identifies the MaxWidth dependency property.
Public fieldStatic memberSupported by Silverlight for Windows Phone MinWidthProperty Identifies the MinWidth dependency property.
Public fieldStatic memberSupported by Silverlight for Windows Phone WidthProperty Identifies the Width dependency property.

Top

Remarks

You can set a ColumnDefinition for each column in a Grid. If you do not specify a ColumnDefinition, by default the Grid will contain a single column with star sized width.

To set a group of ColumnDefinition elements by using code, add the ColumnDefinition to a ColumnDefinitionCollection.

ColumnDefinition uses a GridLength structure to describe its width characteristics. GridLength allows a ColumnDefinition to support variable distribution of available space.

Examples

The following example uses three ColumnDefinitions and three RowDefinitions to create a grid that has nine cells, such as in a worksheet. Each cell contains a TextBlock element that represents data, and the top row contains a TextBlock with the ColumnSpan property applied. To show the boundaries of each cell, the ShowGridLines property is enabled.

<Grid Background="White" VerticalAlignment="Top" HorizontalAlignment="Left" ShowGridLines="True" Width="350" Height="200">
    <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition />
        <RowDefinition />
        <RowDefinition />
    </Grid.RowDefinitions>

    <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="20" FontWeight="Bold" Grid.ColumnSpan="3" Grid.Row="0">Expenses for 2007</TextBlock>
    <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="15" FontWeight="Bold" Grid.Row="1" Grid.Column="0">Travel</TextBlock>
    <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="15" FontWeight="Bold" Grid.Row="1" Grid.Column="1">Office Supplies</TextBlock>
    <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="15" FontWeight="Bold" Grid.Row="1" Grid.Column="2">Salary</TextBlock>
    <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Row="2" Grid.Column="0">20000</TextBlock>
    <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Row="2" Grid.Column="1">10000</TextBlock>
    <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Row="2" Grid.Column="2">50000</TextBlock>
    <TextBlock FontSize="16" FontWeight="Bold" Grid.ColumnSpan="3" Grid.Row="3">Total Expense: 80000</TextBlock>
</Grid>
Public Sub New()
    InitializeComponent()
    CreateGrid()
End Sub

Private Sub CreateGrid()
    ' Create the Grid 
    Dim myGrid As New Grid()
    myGrid.Width = 350
    myGrid.Height = 200
    myGrid.HorizontalAlignment = HorizontalAlignment.Left
    myGrid.VerticalAlignment = VerticalAlignment.Top
    myGrid.ShowGridLines = True
    Dim myBrush As New SolidColorBrush(Colors.White)
    myGrid.Background = myBrush

    ' Define the Columns 
    Dim colDef1 As New ColumnDefinition()
    Dim colDef2 As New ColumnDefinition()
    Dim colDef3 As New ColumnDefinition()
    myGrid.ColumnDefinitions.Add(colDef1)
    myGrid.ColumnDefinitions.Add(colDef2)
    myGrid.ColumnDefinitions.Add(colDef3)

    ' Define the Rows 
    Dim rowDef1 As New RowDefinition()
    Dim rowDef2 As New RowDefinition()
    Dim rowDef3 As New RowDefinition()
    Dim rowDef4 As New RowDefinition()
    myGrid.RowDefinitions.Add(rowDef1)
    myGrid.RowDefinitions.Add(rowDef2)
    myGrid.RowDefinitions.Add(rowDef3)
    myGrid.RowDefinitions.Add(rowDef4)

    ' Add the first text cell to the Grid 
    Dim txt1 As New TextBlock()
    txt1.Text = "Expenses for 2007"
    txt1.FontSize = 20
    txt1.FontWeight = FontWeights.Bold
    Grid.SetColumnSpan(txt1, 3)
    Grid.SetRow(txt1, 0)
    txt1.HorizontalAlignment = HorizontalAlignment.Center
    txt1.VerticalAlignment = VerticalAlignment.Center

    ' Add the second text cell to the Grid 
    Dim txt2 As New TextBlock()
    txt2.Text = "Travel"
    txt2.FontSize = 15
    txt2.FontWeight = FontWeights.Bold
    Grid.SetRow(txt2, 1)
    Grid.SetColumn(txt2, 0)
    txt2.HorizontalAlignment = HorizontalAlignment.Center
    txt2.VerticalAlignment = VerticalAlignment.Center

    ' Add the third text cell to the Grid 
    Dim txt3 As New TextBlock()
    txt3.Text = "Office Supplies"
    txt3.FontSize = 15
    txt3.FontWeight = FontWeights.Bold
    Grid.SetRow(txt3, 1)
    Grid.SetColumn(txt3, 1)
    txt3.HorizontalAlignment = HorizontalAlignment.Center
    txt3.VerticalAlignment = VerticalAlignment.Center

    ' Add the fourth text cell to the Grid 
    Dim txt4 As New TextBlock()
    txt4.Text = "Salary"
    txt4.FontSize = 15
    txt4.FontWeight = FontWeights.Bold
    Grid.SetRow(txt4, 1)
    Grid.SetColumn(txt4, 2)
    txt4.HorizontalAlignment = HorizontalAlignment.Center
    txt4.VerticalAlignment = VerticalAlignment.Center

    ' Add the sixth text cell to the Grid 
    Dim txt5 As New TextBlock()
    Dim db1 As New Double()
    db1 = 20000
    txt5.Text = db1.ToString()
    Grid.SetRow(txt5, 2)
    Grid.SetColumn(txt5, 0)
    txt5.HorizontalAlignment = HorizontalAlignment.Center
    txt5.VerticalAlignment = VerticalAlignment.Center

    ' Add the seventh text cell to the Grid 
    Dim txt6 As New TextBlock()
    Dim db2 As New Double()
    db2 = 10000
    txt6.Text = db2.ToString()
    Grid.SetRow(txt6, 2)
    Grid.SetColumn(txt6, 1)
    txt6.HorizontalAlignment = HorizontalAlignment.Center
    txt6.VerticalAlignment = VerticalAlignment.Center

    ' Add the final text cell to the Grid 
    Dim txt7 As New TextBlock()
    Dim db3 As New Double()
    db3 = 50000
    txt7.Text = db3.ToString()
    Grid.SetRow(txt7, 2)
    Grid.SetColumn(txt7, 2)
    txt7.HorizontalAlignment = HorizontalAlignment.Center
    txt7.VerticalAlignment = VerticalAlignment.Center

    ' Total all Data and Span Three Columns 
    Dim txt8 As New TextBlock()
    txt8.FontSize = 16
    txt8.FontWeight = FontWeights.Bold
    txt8.Text = "Total Units: " + (db1 + db2 + db3).ToString()
    Grid.SetRow(txt8, 3)
    Grid.SetColumnSpan(txt8, 3)

    ' Add the TextBlock elements to the Grid Children collection 
    myGrid.Children.Add(txt1)
    myGrid.Children.Add(txt2)
    myGrid.Children.Add(txt3)
    myGrid.Children.Add(txt4)
    myGrid.Children.Add(txt5)
    myGrid.Children.Add(txt6)
    myGrid.Children.Add(txt7)
    myGrid.Children.Add(txt8)

    LayoutRoot.Children.Add(myGrid)

End Sub
public Page()
{
    InitializeComponent();
    CreateGrid();
}

private void CreateGrid()
{
    // Create the Grid
    Grid myGrid = new Grid();
    myGrid.Width = 350;
    myGrid.Height = 200;
    myGrid.HorizontalAlignment = HorizontalAlignment.Left;
    myGrid.VerticalAlignment = VerticalAlignment.Top;
    myGrid.ShowGridLines = true;
    SolidColorBrush myBrush = new SolidColorBrush(Colors.White);
    myGrid.Background = myBrush;

    // Define the Columns
    ColumnDefinition colDef1 = new ColumnDefinition();
    ColumnDefinition colDef2 = new ColumnDefinition();
    ColumnDefinition colDef3 = new ColumnDefinition();
    myGrid.ColumnDefinitions.Add(colDef1);
    myGrid.ColumnDefinitions.Add(colDef2);
    myGrid.ColumnDefinitions.Add(colDef3);

    // Define the Rows
    RowDefinition rowDef1 = new RowDefinition();
    RowDefinition rowDef2 = new RowDefinition();
    RowDefinition rowDef3 = new RowDefinition();
    RowDefinition rowDef4 = new RowDefinition();
    myGrid.RowDefinitions.Add(rowDef1);
    myGrid.RowDefinitions.Add(rowDef2);
    myGrid.RowDefinitions.Add(rowDef3);
    myGrid.RowDefinitions.Add(rowDef4);

    // Add the first text cell to the Grid
    TextBlock txt1 = new TextBlock();
    txt1.Text = "Expenses for 2007";
    txt1.FontSize = 20;
    txt1.FontWeight = FontWeights.Bold;
    Grid.SetColumnSpan(txt1, 3);
    Grid.SetRow(txt1, 0);
    txt1.HorizontalAlignment = HorizontalAlignment.Center;
    txt1.VerticalAlignment = VerticalAlignment.Center;

    // Add the second text cell to the Grid
    TextBlock txt2 = new TextBlock();
    txt2.Text = "Travel";
    txt2.FontSize = 15;
    txt2.FontWeight = FontWeights.Bold;
    Grid.SetRow(txt2, 1);
    Grid.SetColumn(txt2, 0);
    txt2.HorizontalAlignment = HorizontalAlignment.Center;
    txt2.VerticalAlignment = VerticalAlignment.Center;

    // Add the third text cell to the Grid
    TextBlock txt3 = new TextBlock();
    txt3.Text = "Office Supplies";
    txt3.FontSize = 15;
    txt3.FontWeight = FontWeights.Bold;
    Grid.SetRow(txt3, 1);
    Grid.SetColumn(txt3, 1);
    txt3.HorizontalAlignment = HorizontalAlignment.Center;
    txt3.VerticalAlignment = VerticalAlignment.Center;

    // Add the fourth text cell to the Grid
    TextBlock txt4 = new TextBlock();
    txt4.Text = "Salary";
    txt4.FontSize = 15;
    txt4.FontWeight = FontWeights.Bold;
    Grid.SetRow(txt4, 1);
    Grid.SetColumn(txt4, 2);
    txt4.HorizontalAlignment = HorizontalAlignment.Center;
    txt4.VerticalAlignment = VerticalAlignment.Center;

    // Add the sixth text cell to the Grid
    TextBlock txt5 = new TextBlock();
    Double db1 = new Double();
    db1 = 20000;
    txt5.Text = db1.ToString();
    Grid.SetRow(txt5, 2);
    Grid.SetColumn(txt5, 0);
    txt5.HorizontalAlignment = HorizontalAlignment.Center;
    txt5.VerticalAlignment = VerticalAlignment.Center;

    // Add the seventh text cell to the Grid
    TextBlock txt6 = new TextBlock();
    Double db2 = new Double();
    db2 = 10000;
    txt6.Text = db2.ToString();
    Grid.SetRow(txt6, 2);
    Grid.SetColumn(txt6, 1);
    txt6.HorizontalAlignment = HorizontalAlignment.Center;
    txt6.VerticalAlignment = VerticalAlignment.Center;

    // Add the final text cell to the Grid
    TextBlock txt7 = new TextBlock();
    Double db3 = new Double();
    db3 = 50000;
    txt7.Text = db3.ToString();
    Grid.SetRow(txt7, 2);
    Grid.SetColumn(txt7, 2);
    txt7.HorizontalAlignment = HorizontalAlignment.Center;
    txt7.VerticalAlignment = VerticalAlignment.Center;

    // Total all Data and Span Three Columns
    TextBlock txt8 = new TextBlock();
    txt8.FontSize = 16;
    txt8.FontWeight = FontWeights.Bold;
    txt8.Text = "Total Units: " + (db1 + db2 + db3).ToString();
    Grid.SetRow(txt8, 3);
    Grid.SetColumnSpan(txt8, 3);

    // Add the TextBlock elements to the Grid Children collection
    myGrid.Children.Add(txt1);
    myGrid.Children.Add(txt2);
    myGrid.Children.Add(txt3);
    myGrid.Children.Add(txt4);
    myGrid.Children.Add(txt5);
    myGrid.Children.Add(txt6);
    myGrid.Children.Add(txt7);
    myGrid.Children.Add(txt8);

    LayoutRoot.Children.Add(myGrid);

}

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

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.