I'm currently working on dynamically inserting Textblocks into Stackpanels. This has to be done numerous times, and there is no way to know what the size of the Stackpanel will be beforehand.
Currently, I have something like this:
TextBlock tmp = new TextBlock {
Text = curField.FieldName,
Foreground = new SolidColorBrush(Colors.Red),
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center
};
tmp.MouseLeftButtonUp += imgFormImage_MouseLeftButtonUp;
curField.assocStackpanel.Children.Add(tmp);
curField.assocStackpanel = curField.assocStackpanel;
SelectedFields.Add(curField);
Right now, the textblock just shows up centered horizontally in the Stackpanel, but not vertically. So I need to fix that. Also, ideally I would like to be able to dynamically determine the Textblock's font size so that it will fill the available space. Right now I think it's just taking the default of (I believe) 10.