C# 是否有一种方法可以获取当前列和行索引对,其中矩形在统一的网格上单击,并带有for循环等。?
虽然矩形放置在UniformGrid中,但可以为它们指定Grid.Row和Grid.Column属性: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
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;
}