Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 是否有一种方法可以获取当前列和行索引对,其中矩形在统一的网格上单击,并带有for循环等。?_C#_Wpf_Conways Game Of Life - Fatal编程技术网

C# 是否有一种方法可以获取当前列和行索引对,其中矩形在统一的网格上单击,并带有for循环等。?

C# 是否有一种方法可以获取当前列和行索引对,其中矩形在统一的网格上单击,并带有for循环等。?,c#,wpf,conways-game-of-life,C#,Wpf,Conways Game Of Life,虽然矩形放置在UniformGrid中,但可以为它们指定Grid.Row和Grid.Column属性: private void ToggleGrid(object sender, MouseButtonEventArgs e) //changes color { if (e.OriginalSource is Shape s) { cellColumn = (int)GetBoundingBox(s, gameUnifor

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

    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);
    }
}
然后读它们:

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

使用
Rectangle.Tag
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;
}