Search code examples
c#wpfuniformgrid

C# fill Dynamic Grid with TextBox, Button and more in WPF


  • VS2010 -C# I have a TextBox where you can input an integer and a UniformGrid is dynamically generated with the integer number of UniformGrid Cells. Now if I want to add a TextBox to each UniformGrid Cell, I do the following (e.g. int is a 5):

TextBox[] tb = new TextBox[5];
for (int i = 0; i < 5; i++)
{
tb[i] = new Textbox();
UniformGrid1.Children.Add(tb[i]);
}

Ok..that works fine. But problem comes when I need also to fill in some more Texboxes, Buttons, and Labels in it with some design properties defined, possibly also insert a grid inside a UniformGrid Cell. It will get extremely messy if I create arrays for each control and define each array properties in the loop. Not only that it's messy, I cannot put the textbox inside the UniformGrid's Cell's Grid. The children add the grid on top of the TextBox instead.

Is there a better way to approach this?


Solution

  • I think that using specialized controls like DataGrid would be better, beacuse it automatically creates two-way bindings to model, it's easy to maintain, etc. There are lots of examples, for example here (Google can help you to find more, if you ask something like "wpf datagrid example tutorial")