Question

I would like to add a custom style to a generated textblock.

        TextBlock title = new TextBlock();
        title.Style = (Style) Application.Current.Resources["styleTheke"];
        title.Text = "test";
        stackMenu.Children.Add(title);

This style is defined in

 <phone:PhoneApplicationPage.Resources>
  <Style x:Key="styleTheke" TargetType="TextBlock">
   <Setter Property="Width" Value="Auto"/>
   <Setter Property="Height" Value="40"/>
   <Setter Property="FontSize" Value="{StaticResource PhoneFontSizeLarge}"/>
   <Setter Property="Foreground" Value="{StaticResource PhoneAccentBrush}"/>
  </Style>
 </phone:PhoneApplicationPage.Resources>

However .. the Textblock always appears "unstyled".

Was it helpful?

Solution

If the resource is in the same page then you can refer to it via:

(Style) Resources["styleTheke"];

The Application.Current.Resources Dictionary is for resources (typically) defined in App.xaml.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top