How do I get cell row and column indexes of UWP Grid control?

做~自己de王妃 提交于 2019-12-25 07:40:19

问题


I've been trying to get the row index and the column index when specific cell of grid control is tapped. So far nothing...

I achieved to take the absolute value of X and Y when I tap somewhere on the grid control.

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

I've tried this:

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

Both gives me only zeros no matter where I tap.

I didn't find another way to take the row and column indexes of the tapped cell...

XAML code:

<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:


Use 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
            }



回答2:


Many thanks to Arcana.

The solution is:

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


来源:https://stackoverflow.com/questions/36277304/how-do-i-get-cell-row-and-column-indexes-of-uwp-grid-control

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!