2010-12-21 75 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