有没有办法让当前列和行索引对是点击了一个统一的网格与循环等矩形哪里?(Is there a way

2019-10-29 23:09发布

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

Answer 1:

虽然矩形被放入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;
}


文章来源: 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.?