Reputation: 1
Well, I am still in the learning phase of XAML and WPF. I want to be able to create my own grids with my own controls. I pass in the function the grid and a list of formats that include the format for example textbox or combobox and the name. I want it to start after every third control in the new line regardless of how many controls are supplied in the list.
Unfortunately, the code does not lead to the goal and looks like this: The Form
Maybe someone knows how to solve it better? It does not look so nice. Is there a way to make look it symetrical and nice?
public Grid GetGridWithMaskControls(Grid grid, List<Format> formats) {
int columnIndex = 0;
int rowIndex = 0;
for(int i = 0; i < formats.Count; i++) {
if(i % 3 == 0) {
rowIndex++;
columnIndex = 0;
}
grid.ColumnDefinitions.Add(new ColumnDefinition());
RowDefinition row = new RowDefinition();
row.Height = new GridLength(25);
grid.RowDefinitions.Add(row);
Label t1 = new Label();
Format format = formats[i];
t1.Content = format.FieldName;
TextBox tb = new TextBox();
tb.Height = 20;
tb.Width = 120;
Grid.SetColumn(tb, i);
Grid.SetRow(tb,rowIndex);
grid.Children.Add(tb);
Grid.SetColumn(t1, i);
Grid.SetRow(t1, rowIndex);
grid.Children.Add(t1);
columnIndex++;
}
return grid;
}
Upvotes: 0
Views: 29
Reputation: 46
The Problem ist that you have not to add the colmundefinition for each row.
I think you want the output like this:
private void GenerateGrid()
{
for (int i = 0; i < 3; i++)
{
grid.ColumnDefinitions.Add(new ColumnDefinition());
}
int columnIndex = 0;
int rowIndex = 0;
for (int i = 0; i < formats.Count; i++)
{
if (i % 3 == 0)
{
rowIndex++;
columnIndex = 0;
RowDefinition row = new RowDefinition();
row.Height = new GridLength(25);
grid.RowDefinitions.Add(row);
}
Label t1 = new Label();
Format format = formats[i];
Grid.SetColumn(t1, i%3);
Grid.SetRow(t1, rowIndex);
grid.Children.Add(t1);
t1.Content = format.FieldName;
TextBox tb = new TextBox();
tb.Height = 20;
tb.Width = 120;
Grid.SetColumn(tb, i%3);
Grid.SetRow(tb, rowIndex);
grid.Children.Add(tb);
columnIndex++;
}
}
Upvotes: 0