Dela via


Grid.GetColumn(FrameworkElement) Method

Definition

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

public:
 static int GetColumn(FrameworkElement ^ element);
 static int GetColumn(FrameworkElement const& element);
public static int GetColumn(FrameworkElement element);
function getColumn(element)
Public Shared Function GetColumn (element As FrameworkElement) As Integer

Parameters

element
FrameworkElement

The element from which to read the property value.

Returns

Int32

int

The value of the Grid.Column XAML attached property on the target element. This is a zero-based index.

Examples

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

<Grid x:Name="LayoutRoot">
    <Grid.Resources>
        <Style TargetType="TextBlock">
            <Setter Property="Foreground" Value="Black"/>
        </Style>
    </Grid.Resources>
    <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" 
               PointerEntered="r1_PointerEntered"/>
    <Rectangle Fill="Yellow" Height="100" Width="100" Grid.Row="0" Grid.Column="1" 
               PointerEntered="r1_PointerEntered" />
    <Rectangle Fill="Blue" Height="100" Width="100" Grid.Row="1" Grid.Column="0" 
               PointerEntered="r1_PointerEntered" />
    <Rectangle Fill="Green" Height="100" Width="100" Grid.Row="1" Grid.Column="1" 
               PointerEntered="r1_PointerEntered"/>
    <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_PointerEntered(object sender, PointerRoutedEventArgs e)
{
    Rectangle r = (Rectangle)sender;
    int row = Grid.GetRow(r);
    int col = Grid.GetColumn(r);

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

Remarks

This method is a utility method for the property system, and isn't used in most app scenarios. In most cases you set the Grid.Column XAML attached property in XAML and won't need this method. For more info, see the Grid.Column XAML attached property.

Applies to

See also