private void drawField(double _rows, double _columns) { Canvas canv = (Canvas)FindName("MainCanvas"); Grid grid = (Grid)FindName("MainGrid"); if (canv.Children.Count > 0) { canv.Children.Clear(); } double column = _columns; double rows = _rows; double posL = 0; double posT = 0; double sizeW = canv.ActualWidth / column; double sizeH = canv.ActualHeight / rows; BtnHolder = new ButtonCard[(int)rows, (int)column]; for (int i = 0; i < rows; i++) { for (int j = 0; j < column; j++) { //create a button at the correct position CanvasButton btn = new CanvasButton(i, j); btn.Background = Brushes.White; btn.Width = sizeW; btn.Height = sizeH; Canvas.SetLeft(btn, posL); Canvas.SetTop(btn, posT); canv.Children.Add(btn); //create a card that stores all important information ButtonCard tmpBtnCard = new ButtonCard(); tmpBtnCard.name = "Button_" + i + "_" + j; tmpBtnCard.btn = btn; tmpBtnCard.btn.Click += new RoutedEventHandler(button_Click); //assigne the ButtonCard to the correct position inside the holder BtnHolder[i, j] = tmpBtnCard; posL += sizeW; } posL = 0; posT += sizeH; } }
/// <summary> /// Depending on the buttons color, a click on it /// will set the given cell at the position to alive or death. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button_Click(object sender, EventArgs e) { CanvasButton button = sender as CanvasButton; if (button.Background == Brushes.Black) { golWorld.setValue(button._row, button._column, 0);//dead button.Background = Brushes.White; //System.Console.WriteLine("button pos x,y: " +button._row + " " + button._column); } else { golWorld.setValue(button._row, button._column, 1);//alive button.Background = Brushes.Black; //System.Console.WriteLine("button pos x,y: " +button._row + " " + button._column); } }