As I am using WPF for this build, I need to setup the grid and the cell template in XAML file. So I can bind the properties like collection and its cell objects. I have chosen my collection will be a collection of rectangles.
I can not just use controls and expect them look the way I need. I need styles.
Like this:
<Style TargetType="ListBox">
<Setter Property="Padding" Value="0"/>
<Setter Property="Margin" Value="0"/>
<Setter Property="BorderThickness" Value="0"/>
</Style>
<Style TargetType="ListBoxItem">
<Setter Property="Padding" Value="0"/>
<Setter Property="Margin" Value="0"/>
<Setter Property="BorderThickness" Value="0"/>
</Style>
As you can see, I will be using a LisrBox control. This portion of code goes into a parent tag called
<Window.Resources> <!--Any reusable resources go here--> </Window.Resources>
That comment there means, any resources that do not need to be reused, do not need to be here. Rather, they can be inserted directly to the controls they need to modify the looks of.
The grid data item source
private void GeneratePixels(int rows, int cols)
{
Rectangles = new List<List<System.Windows.Shapes.Rectangle>>();
for (int i = 0; i < rows; i++) {
var b = new List<System.Windows.Shapes.Rectangle>();
Rectangles.Add(b);
for (int j = 0; j < cols; j++)
{
System.Windows.Shapes.Rectangle r = new System.Windows.Shapes.Rectangle();
r.Width = 240 / cols;
r.Height = 240 / rows;
r.StrokeThickness = 1;
r.Stroke = System.Windows.Media.Brushes.Black;//we need this to see the cell borders. Finalized version does not need to show cell borders
b.Add(r);
}
}
pixelBox.ItemsSource = Rectangles;
}
The code shows the collection that is being fed to the grid being generated according to the passed width and hight values. Width = rows and Height = cols.
The variables used are declared earlier at the start of the MainWindow class. I am not using MVVM pattern at this stage. It is still a proof of concept, a simple prototype project.
I will move the parts out to the relative classes and files when the final conclusions are made and I am happy with results.