Hallo,
Ich versuche per Code ein Grid anzulegen und mit Objekten zu füllen. Wenn ich aber z.B. Rows und Columns mit 12x8 anlege und in einer Schleife in jedes Feld ein Objekt (ein Usercontrol, welches eine Ellipse zeichnet) packen möchte, dann werden immer nur 8x8 angezeigt. Hier erstmal mein Code:
public partial class MWP : UserControl
{
private List<ColumnDefinition> Columns;
private List<RowDefinition> Rows;
public MWP()
{
InitializeComponent();
//create standard grid with size 6x4 (24 wells)
initGrid(12, 8);
}
public MWP(int dimX, int dimY)
{
//create grid with dynamic dimensions
initGrid(dimX, dimY);
}
private void initGrid(int dimX, int dimY)
{
Columns = new List<ColumnDefinition>();
Rows = new List<RowDefinition>();
Wells.Width = 400;
Wells.HorizontalAlignment = HorizontalAlignment.Left;
Wells.VerticalAlignment = VerticalAlignment.Top;
Wells.ShowGridLines = true;
Wells.Background = new SolidColorBrush(Colors.Gray);
// create rows and columns
for (int y = 0; y < dimY; y++)
{
RowDefinition rowDef = new RowDefinition();
rowDef.Height = new GridLength(45);
//rowDef.Height = GridLength.Auto;
Wells.RowDefinitions.Add(rowDef);
}
for (int x = 0; x < dimX; x++)
Wells.ColumnDefinitions.Add(new ColumnDefinition());
//Fill the Grid with wells
for(int y = 0; y < dimY; y++)
for( int x = 0; x < dimX; x++)
AddWellToGrid(y,x);
}
private void AddWellToGrid(int column, int row)
{
Well well = new Well();
well.Tag = column.ToString() + " ; " + row.ToString();
Grid.SetColumn(well, column);
Grid.SetRow(well, row);
if (!Wells.Children.Contains(well))
Wells.Children.Add(well);
Wells.UpdateLayout();
}
Dabei kommt dann ein Grid raus, wie auf dem Bild zu sehen ist. Warum wird nicht in jedes Feld ein Kreis gesetzt?
Hallo,
wenn ich's richtig blicke, hast Du einfach die Koordinaten vertauscht.
AddWellToGrid(y,x); // <-- x und y vertauscht...
AddWellToGrid(int column, int row) // <-- oder column und row vertauscht
Eigentlich ein klassischer Fall für den Debugger.
Gruß, MarsStein
Non quia difficilia sunt, non audemus, sed quia non audemus, difficilia sunt! - Seneca