I really need help. I am dynamically creating a grid control in my code behind, then adding it to the children of the containing control that was defined in the xaml. Now, everything is dynamically being created as expected, but unfortunately when I set the style in the same way I set the text of the textboxes which I add to the grid and position in row/columns accordingly it doens't work. Notice the following code:
AddTextBlock(7, col, String.Format("{0:0}%", finances.PrivateDaysPercent), "GridValueStyle");
TextBlock AddTextBlock( int row, int column, string text, string style)
{
Style s = Resources[style] as Style;
TextBlock tb = new TextBlock() { Text = text};
tb.Style = s;
Grid.SetColumn(tb, column);
Grid.SetRow(tb, row);
grid.Children.Add(tb);
return tb;
}
<Style x:Key="GridValueStyle" TargetType="TextBlock" BasedOn="{StaticResource ContentTextStyle}" >
<Setter Property="Margin" Value="2,1" />
<Setter Property="HorizontalAlignment" Value="Right"/>
<Setter Property="VerticalAlignment" Value="Center" />
</Style>
The style should clearly be setting, but it isn't. The style is defined correctly in the resource dictionary and added to app.xaml. I know it works becuase I use this style in another navigation page and it applies perfectly to a statically created grid in xaml.