2016-03-29 74 views
0

我一直在試圖獲取特定單元格的網格控件被挖掘時行索引和列索引。到目前爲止什麼都沒有...如何獲取UWP Grid控件的單元格行和列索引?

我實現了X和Y的絕對值,當我點擊網格控件的某個地方。

private void gridGameBoard_Tapped(object sender, TappedRoutedEventArgs e) 
{ 
    Grid grd = sender as Grid; 
    Point pos = e.GetPosition(grd); 
} 

我已經試過這樣:

int row = Grid.GetColumn(grd); 
int col = Grid.GetRow(grd); 

兩個給我唯一的零不管我在哪裏挖掘。

我沒有找到另一種方式來取抽頭單元格的行和列索引...

XAML代碼:

<Grid Background="Firebrick"> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="3*" /> 
     <ColumnDefinition Width="1*" /> 
    </Grid.ColumnDefinitions> 

    <Grid Grid.Column="0" Name="gridGameBoard" Tapped="gridGameBoard_Tapped"> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="1*" /> 
      <ColumnDefinition Width="1*" /> 
      <ColumnDefinition Width="1*" /> 
      <ColumnDefinition Width="1*" /> 
      <ColumnDefinition Width="1*" /> 
      <ColumnDefinition Width="1*" /> 
      <ColumnDefinition Width="1*" /> 
     </Grid.ColumnDefinitions> 

     <Grid.RowDefinitions> 
      <RowDefinition Height="1*" /> 
      <RowDefinition Height="1*" /> 
      <RowDefinition Height="1*" /> 
      <RowDefinition Height="1*" /> 
      <RowDefinition Height="1*" /> 
      <RowDefinition Height="1*" /> 
      <RowDefinition Height="1*" /> 
     </Grid.RowDefinitions> 

     <!-- # # # COLUMN 0 # # # --> 
     <Image Grid.Row="0" Grid.Column="0" Source="Assets/box.png" Height="51" Width="69" /> 
     <Image Grid.Row="0" Grid.Column="0" Name="img00" VerticalAlignment="Center" HorizontalAlignment="Center" Stretch="None" /> 

     <Image Grid.Row="1" Grid.Column="0" Source="Assets/box.png" Height="51" Width="69" /> 
     <Image Grid.Row="1" Grid.Column="0" Name="img01" VerticalAlignment="Center" HorizontalAlignment="Center" Stretch="None" /> 

     <Image Grid.Row="2" Grid.Column="0" Source="Assets/box.png" Height="51" Width="69" /> 
     <Image Grid.Row="2" Grid.Column="0" Name="img02" VerticalAlignment="Center" HorizontalAlignment="Center" Stretch="None" /> 

     <Image Grid.Row="3" Grid.Column="0" Source="Assets/box.png" Height="51" Width="69" /> 
     <Image Grid.Row="3" Grid.Column="0" Name="img03" VerticalAlignment="Center" HorizontalAlignment="Center" Stretch="None" /> 

     <Image Grid.Row="4" Grid.Column="0" Source="Assets/box.png" Height="51" Width="69" /> 
     <Image Grid.Row="4" Grid.Column="0" Name="img04" VerticalAlignment="Center" HorizontalAlignment="Center" Stretch="None" /> 

     <Image Grid.Row="5" Grid.Column="0" Source="Assets/box.png" Height="51" Width="69" /> 
     <Image Grid.Row="5" Grid.Column="0" Name="img05" VerticalAlignment="Center" HorizontalAlignment="Center" Stretch="None" /> 

     <!-- # # # COLUMN 1 - 6 are same as COLUMN 0 # # # --> 

    </Grid> 
</Grid> 

回答

1

使用Grid.GetColumn(grd);

private void gridGameBoard_Tapped(object sender, TappedRoutedEventArgs e) 
      { 
       Grid grd = sender as Grid; 
      int i = Grid.GetColumn(grd);//To get column no 
      Grid.GetRow(grd);//To get row no 
      } 
+0

阿奇納, 謝謝你的快速重放。 但我忘了提及我已經試過這個 'int row = Grid.GetColumn(grd);' 和 'int col = Grid.GetRow(grd);'。 無論我在哪裏點擊,兩者都只給我零。 –

+0

它給出了正確的值。您的網格位於第0行和第0列 – Archana

+0

您是否想要獲取網格控件的列值? – Archana

0

非常感謝到Arcana。

解決的辦法是:

<Image Grid.Row="0" Grid.Column="0" Source="Assets/img.png" Tapped="image_Tapped" /> 

private void image_Tapped(object sender, TappedRoutedEventArgs e) 
{ 
    Image img = sender as Image; 

    int row = Grid.GetRow(img); 
    int col = Grid.GetColumn(img); 
} 
相關問題