private void InitializeGrid() { Trace.WriteLine("Initializing Grid"); int numRows = this.ViewModel.GridWidth; int numCols = this.ViewModel.GridHeight; double cellWidth = this.gameOfLifeCanvas.Width / numCols; double cellHeight = this.gameOfLifeCanvas.Height / numRows; CellStateToBrushConverter cellStateToBrushConverter = new CellStateToBrushConverter(); for (int columnIndex = 0; columnIndex < numCols; columnIndex++) { for (int rowIndex = 0; rowIndex < numRows; rowIndex++) { Rectangle cellRect = new Rectangle { Width = cellWidth, Height = cellHeight, RenderTransform = new TranslateTransform(columnIndex * cellWidth, rowIndex * cellHeight), RadiusX = cellWidth / 10, RadiusY = cellHeight / 10 }; CellContainer cellContainer = new CellContainer(this.ViewModel[rowIndex, columnIndex]); cellRect.InputBindings.Add(new MouseBinding(this.ViewModel.CellClickedCommand, new MouseGesture(MouseAction.LeftClick)) { CommandParameter = cellContainer }); cellRect.SetBinding(Shape.FillProperty, new Binding("State") { Source = cellContainer, Mode = BindingMode.OneWay, UpdateSourceTrigger = UpdateSourceTrigger.Explicit, Converter = cellStateToBrushConverter }); this.gameOfLifeCanvas.Children.Add(cellRect); } } Trace.WriteLine("Grid Initialized"); }
private void CellClicked(CellContainer container) { container.FlipCellState(); this.UpdateProperties(); }