Condividi tramite


Grid.GetColumn Method

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

Gets the value of the Grid.Column attached property from the specified FrameworkElement.

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

Syntax

'Declaration
Public Shared Function GetColumn ( _
    element As FrameworkElement _
) As Integer
public static int GetColumn(
    FrameworkElement element
)

Parameters

Return Value

Type: System.Int32
The value of the Grid.Column attached property. This is a zero-based index.

Remarks

Call GetColumn to determine which column an element is placed in for layout.

Examples

The following example shows how to get the row and column of the element that raised an event.

Run this sample

<Grid x:Name="LayoutRoot" Background="White" ShowGridLines="True" >
    <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition />  
    </Grid.RowDefinitions>

    <Rectangle Fill="White" Height="100" Width="100" Grid.Row="0" Grid.Column="0" MouseEnter="r1_MouseEnter"/>
    <Rectangle Fill="Yellow" Height="100" Width="100" Grid.Row="0" Grid.Column="1" MouseEnter="r1_MouseEnter" />
    <Rectangle Fill="Blue" Height="100" Width="100" Grid.Row="1" Grid.Column="0" MouseEnter="r1_MouseEnter" />
    <Rectangle Fill="Green" Height="100" Width="100" Grid.Row="1" Grid.Column="1" MouseEnter="r1_MouseEnter"/>
    <StackPanel >
        <StackPanel Orientation="Horizontal" >
        <TextBlock Text="Row = " />
        <TextBlock x:Name="txtRow"  />
    </StackPanel>
    <StackPanel Orientation="Horizontal" >
        <TextBlock Text="Column = " />
        <TextBlock x:Name="txtCol"  />
    </StackPanel>
        </StackPanel>
</Grid>
private void r1_MouseEnter(object sender, MouseEventArgs e)
{
    Rectangle r = (Rectangle)sender;
    int row = Grid.GetRow(r);
    int col = Grid.GetColumn(r);

    txtRow.Text = row.ToString();
    txtCol.Text = col.ToString();

}

The preceding example produces output that is similar to the following illustration.

Shows the row and column of the selected grid cell

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.