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

牧云@^-^@ 提交于 2019-12-11 15:15:17

问题


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

On the ToggleGrid function, I wanted to set cellColumn and cellRow variable to the value of the column and row of where the clicked rectangle is on a uniform grid and be able to change the rectangle's color using the index value of those variables. I did some research on ways I can go about this and couldn't find anything useful.

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

回答1:


though rectangles are placed into UniformGrid, it is possible to assign Grid.Row and Grid.Column properties for them:

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

then read them:

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


来源:https://stackoverflow.com/questions/51807534/is-there-a-way-to-get-current-column-and-row-index-pair-of-where-rectangle-is-cl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!