How do I generate WPF controls through code

WPF makes use of a funky thing called attached properties. So in your XAML you might write this:

<TextBlock Grid.Row="0" Grid.Column="0" />

And this will effectively move the TextBlock into cell (0,0) of your grid.

In code this looks a little strange. I believe it'd be something like:

g.Children.Add(tb);
Grid.SetRow(tb, 0);
Grid.SetColumn(tb, 0);

Have a look at that link above - attached properties make things really easy to do in XAML perhaps at the expense of intuitive-looking code.

Tags:

C#

.Net

Wpf

Xaml