我一直在尝试获取网格控件的特定单元格时的行索引和列索引。到目前为止没有......
当我点击网格控件上的某个位置时,我实现了取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>
答案 0 :(得分: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
}
答案 1 :(得分:0)
<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);
}