Grid.GetColumn(FrameworkElement) 方法

定義

從指定的FrameworkElement取得Grid.Column XAML 附加屬性的值。

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

參數

element
FrameworkElement

要從中讀取屬性值的項目。

傳回

Int32

int

目標專案上 Grid.Column XAML 附加屬性的值。 這是以零起始的索引。

範例

下列範例示範如何取得引發事件之 元素的資料列和資料行。

<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();
    
}

備註

這個方法是屬性系統的公用程式方法,而且不會在大部分的應用程式案例中使用。 在大部分情況下,您會在 XAML 中設定 Grid.Column XAML 附加屬性,而且不需要此方法。 如需詳細資訊,請參閱 Grid.Column XAML 附加屬性。

適用於

另請參閱