On the last line shown:
// Dynamically add Grid with 8 columns to the StackPanel
Grid grd = new Grid();
ColumnDefinition c0 = new ColumnDefinition();
c0.Width = new GridLength(1, GridUnitType.Star);
. . .
grd.ColumnDefinitions.Add(c0);
. . .
// Add it to the StackPanel
spNufan.Children.Add(grd); // <- ka-pow!
…I get, “System.NullReferenceException was unhandled Message=Object reference not set to an instance of an object.“
Yet “grd” is created in the line:
Grid grd = new Grid();
…and spNufan (a StackPanel) is in the corresponding xaml:
<StackPanel Name="spNufan"
Orientation="Vertical">
</StackPanel>
Why the error?
I would say that your code is perfectly fine, but, in my opinion, you execute it before actually
XAMLcontent is loaded and initialized, so I suppose that yourCause the code provided as is, seems perfectly valid to me. So consider it moving in different place of your app.
I would suggest to check this by adding a button and execute that Stack population code on
Clickevent.Hope this helps.