jeudi 2 avril 2015

How do I dynamtically add and position CheckBox elements in WPF?

So here is the deal, I started making a WinForms application to display a grid of 4*4*4 checkboxes, representing a real-life 4^3 grid of LEDs.


This is what it looks like right now: http://ift.tt/1Cc5o61


I want to convert this to an WPF app, so that I can use the transparency option of the WPF Checkbox, to make the non-selected layer [there will be 4 layers] of checkboxes slightly transparent, to give it a more true 3D feeling.


I am new to WPF and I have tried to nest 2 elements in the main Window [for example, 2 grids to slightly overlap the position, by maybe 20 pixels, so that they would appear to be 3d-staggered], but it simply won't let me do that, and only lets me add a child grid inside the original grid.


TLDR: How can I dynamically create lots of checkboxes while giving them absolute pixel positions?


My current working WinForms C# code:



int spacing = 25;
for (int z = 1; z <= 4; z++)
{
List<string> zString = new List<string>();
for (int y = 1; y <= 4; y++)
{
for (int x = 1; x <= 4; x++)
{

int pixel_x = zero + ((x - 1) * spacing);
int pixel_y = (zero - 4) + ((y - 1) * spacing);

//int id = ((y - 1) * 4) + x;

CheckBox box = new CheckBox();
box.CheckStateChanged += new System.EventHandler(checkBox2_CheckedChanged);
box.Tag = id;
zString.Add(id.ToString());
//box.Text = id.ToString();
box.BackColor = Color.Transparent;
//box.
box.AutoSize = false;
box.Size = new Size(20, 20);
box.Padding = new Padding(3);
box.Location = new Point(pixel_x, pixel_y);

this.Controls.Add(box);
id++;
}
}




Aucun commentaire:

Enregistrer un commentaire