2010-12-21 82 views
0

我想将自定义样式添加到生成的文本块。将样式添加到生成的TextBlock

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

这种风格在

<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> 

限定。然而..正文块总是出现 “无样式”。

回答

3

如果资源是在同一个页面,那么你可以通过引用它:

(Style) Resources["styleTheke"]; 

Application.Current.Resources字典是对资源(典型值)的App.xaml定义。

+0

我早些时候尝试过。一旦我的程序达到这一点,我会得到一个异常,指出“未指定的错误”。 (这与此相同。资源.. – theXs 2010-12-21 14:17:45