如何获取 UWP Grid 控件的单元格行和列索引? [英] How do I get cell row and column indexes of UWP Grid control?

查看:26
本文介绍了如何获取 UWP Grid 控件的单元格行和列索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当点击网格控件的特定单元格时,我一直在尝试获取行索引和列索引.到目前为止什么都没有...

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

当我点击网格控件上的某处时,我取得了 X 和 Y 的绝对值.

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

我已经试过了:

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

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

推荐答案

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
            }

这篇关于如何获取 UWP Grid 控件的单元格行和列索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆