0

I am trying to draw a series of squares in C#. This is my first foray into WPF. My SpaceProbeView class has a SetModel function that allows the user to pass the data on the squares that will be rendered.

Once the model has been set, SpaceProbeView iterates through the 2D array that is the model. Each cell has data on what type of space it is, and the SpaceProbeView sets the color of the square to draw based on the space type.

With the code I have right now it just keeps drawing the squares at the same position, and I've been looking through documentation and can't figure out how to adjust the position of each Rectangle.

Here is my SpaceProbeView:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;

namespace SpaceProbe
{
    public class SpaceProbeView
    {
        Window window = new Window();
        Canvas canvas = new Canvas();
        private int cellSize = 25;
        SpaceProbeModel model;
        int rowAndColumnSize;
        SolidColorBrush emptySpaceBrush = new SolidColorBrush();
        SolidColorBrush asteroidBrush = new SolidColorBrush();
        SolidColorBrush gravityWellBrush = new SolidColorBrush();
        SolidColorBrush gravityInfluencedBrush = new SolidColorBrush();
        Brush outlineBrush = System.Windows.Media.Brushes.Black;

        public SpaceProbeView(int size)
        {
            emptySpaceBrush.Color = Colors.Blue;
            asteroidBrush.Color = Colors.Brown;
            gravityWellBrush.Color = Colors.LightGray;
            gravityInfluencedBrush.Color = Colors.Gray;

            rowAndColumnSize = size;
            window.Title = "Space Probe";
            canvas.Width = rowAndColumnSize * cellSize;
            canvas.Height = rowAndColumnSize * cellSize;
            window.Width = rowAndColumnSize * cellSize;
            window.Height = rowAndColumnSize * cellSize;
            window.Content = canvas;

            window.Show();
        }

        public void SetModel(SpaceProbeModel aModel)
        {
            model = aModel;
            for (int row = 0; row < rowAndColumnSize; row++)
            {
                for (int column = 0; column < rowAndColumnSize; column++)
                {
                    Rectangle rect = new Rectangle();
                    rect.Width = cellSize;
                    rect.Height = cellSize;
                    rect.Stroke = outlineBrush;
                    rect.RenderTransform = new Point(row * cellSize, column * cellSize);

                    switch (model.cellType[row, column])
                    {
                        case SpaceProbeModel.type.asteroid:
                            rect.Fill = asteroidBrush;
                            break;
                        case SpaceProbeModel.type.gravityInfluenced:
                            rect.Fill = gravityInfluencedBrush;
                            break;
                        case SpaceProbeModel.type.gravityWell:
                            rect.Fill = gravityWellBrush;
                            break;
                        case SpaceProbeModel.type.empty:
                            rect.Fill = emptySpaceBrush;
                            break;
                    }
                    canvas.Children.Add(rect);
                }
            }
        }
    }
}
ThomYorkkke
  • 2,061
  • 3
  • 18
  • 26
  • 7
    Delete all that and use proper XAML. – Federico Berasategui Jan 24 '15 at 22:48
  • @HighCore Seriously? Is that how it's done? I know nothing about WPF. – ThomYorkkke Jan 24 '15 at 22:59
  • 2
    Yep. Seriously. Start reading [here](https://msdn.microsoft.com/en-us/library/ms752347(v=vs.110).aspx) and [here](http://drwpf.com/blog/itemscontrol-a-to-z/) and see my examples [1](http://stackoverflow.com/a/15580293/643085) [2](http://stackoverflow.com/a/15469477/643085) [3](http://stackoverflow.com/a/15821573/643085) [4](http://stackoverflow.com/a/16947081/643085) about how to create diagrams / shapes items-based UIs using WPF best practices such as DataBinding, ItemsControls and DataTemplates. – Federico Berasategui Jan 24 '15 at 23:08
  • 1
    @HighCore Okay cool. – ThomYorkkke Jan 24 '15 at 23:18

0 Answers0