有没有办法获取当前列和行索引对,其中矩形在带有 for 循环等的统一网格上单击?

Is there a way to get current column and row index pair of where rectangle is clicked on a uniform grid with for loop, etc.?

namespace ConwayGameofLife
{
    public partial class GameController : Window
    {
        public int row;
        public int col;
        public SolidColorBrush brush;
        public Rectangle[,] rectangle;
        public GridCell cell;
        public string[,] cellPosition;
        public int cellColumn;
        public int cellRow;

    public GameController()
    {
        InitializeComponent();
        brush = new SolidColorBrush(Colors.Gray);
    }

    private void GridSlider_Button(object sender, RoutedPropertyChangedEventArgs<double> e)
    {
        row = (int)gridSlider.Value;
        col = (int)gridSlider.Value;
        resizeGrid.DataContext = gridSlider;
    }

    private void BuildGrid_Button(object sender, RoutedEventArgs e)
    {
        gameUniformGrid.Children.Clear();
        gameUniformGrid.Rows = row;
        gameUniformGrid.Columns = col;
        rectangle = new Rectangle[row, col];


        for (int x = 0; x < row; x++)
        {
            for (int y = 0; y < col; y++)
            {
                rectangle[y, x] = new Rectangle { Stroke = Brushes.Black, Fill = brush };
                gameUniformGrid.Children.Add(rectangle[y, x]);
            }
        }

        //for (int i = 0; i < rectangle.Length; i++)
        //{
        //    rectangle[i] = new Rectangle { Stroke = Brushes.Black, Fill = brush };

        //    gameUniformGrid.Children.Add(rectangle[i]);
        //}
    }

在 ToggleGrid 函数中,我想将 cellColumn 和 cellRow 变量设置为单击的矩形在统一网格上的列和行的值,并能够使用它们的索引值更改矩形的颜色变量。我对解决此问题的方法进行了一些研究,但找不到任何有用的东西。

    private void ToggleGrid(object sender, MouseButtonEventArgs e) //changes color
    {
        if (e.OriginalSource is Shape s)
        {
            cellColumn = (int)GetBoundingBox(s, gameUniformGrid).X;
            cellRow = (int)GetBoundingBox(s, gameUniformGrid).Y;
            rectangle[cellColumn, cellRow].Fill = new SolidColorBrush(Colors.Green);
            //s.Fill = new SolidColorBrush(Colors.Green);
            //cellPosition[y, x] = "Dead";
        }
        MessageBox.Show($"Column Clicked: {cellColumn}, Row Clicked: {cellRow}");
    }

    private static Rect GetBoundingBox(FrameworkElement element, FrameworkElement parent)
    {
        GeneralTransform transform = element.TransformToAncestor(parent);
        Point topLeft = transform.Transform(new Point(0, 0));
        Point bottomRight = transform.Transform(new Point(element.ActualWidth, element.ActualHeight));
        return new Rect(topLeft, bottomRight);
    }
}

虽然矩形被放置在 UniformGrid 中,但可以为它们分配 Grid.Row 和 Grid.Column 属性:

rectangle[x, y] = new Rectangle { Stroke = Brushes.Black, Fill = brush };
rectangle[x, y].SetValue(Grid.RowProperty, x);
rectangle[x, y].SetValue(Grid.ColumnProperty, y);
gameUniformGrid.Children.Add(rectangle[x, y]);

然后阅读它们:

if (e.OriginalSource is Shape s)
{
    cellColumn = (int)s.GetValue(Grid.ColumnProperty);
    cellRow = (int)s.GetValue(Grid.RowProperty);
    rectangle[cellRow, cellColumn].Fill = Brushes.Green;
}