2011-04-26 62 views
3

在Silverlight 4中(使用Expression Blend 4),如何在包含它的Border的樣式中更改TextBox的字體大小?我將風格從WPF轉換爲Silverlight(總是很有趣)。這是我有:將樣式應用於Silverlight 4中的子元素

<Style x:Key="Title" TargetType="Border"> 
    <Setter Property="TextBlock.VerticalAlignment" Value="Center"/> 
    <Setter Property="TextBlock.TextAlignment" Value="Center"/> 
    <Setter Property="TextBlock.FontSize" Value="48"/> 
    <Setter Property="TextBlock.Foreground" Value="{StaticResource TextForeground}"/> 
    <Setter Property="VerticalAlignment" Value="Top"/> 
    <Setter Property="HorizontalAlignment" Value="Stretch"/> 
    <Setter Property="Background" Value="{StaticResource TitleBackground}"/> 
    <Setter Property="Padding" Value="25,0"/> 
</Style> 

它不工作。它給我的設計師以下異常: enter image description here

編輯:

好吧,我知道這是可能在WPF。在Silverlight中是不可能的(沒有像Xin建議的那樣採用整個主題構造)

+0

我不認爲這是可以做到這一點在Silverlight 4 ... – 2011-04-27 02:14:28

+0

@辛,不是很有幫助,但誠實。謝謝:) – Jordan 2011-04-27 14:51:44

回答

0

實際上,您可能可以從silverlight工具包主題中獲得所需內容。你可以找到它here(主題 - >主題瀏覽器)。

更新:

首先,你需要創建從主題(System.Windows.Controls.Theming)繼承的類。我基本上從源代碼複製並重命名它。

/// <summary> 
    /// Implicitly applies the border theme to all of its descendent 
    /// FrameworkElements. 
    /// </summary> 
    /// <QualityBand>Preview</QualityBand> 
    public class BorderTheme : Theme 
    { 
     /// <summary> 
     /// Stores a reference to a Uri referring to the theme resource for the class. 
     /// </summary> 
     private static Uri ThemeResourceUri = new Uri("/theming;component/Theme.xaml", UriKind.Relative); 

     /// <summary> 
     /// Initializes a new instance of the ExpressionDarkTheme class. 
     /// </summary> 
     public BorderTheme() 
      : base(ThemeResourceUri) 
     { 
      var a = ThemeResourceUri; 
     } 

     /// <summary> 
     /// Gets a value indicating whether this theme is the application theme. 
     /// </summary> 
     /// <param name="app">Application instance.</param> 
     /// <returns>True if this theme is the application theme.</returns> 
     public static bool GetIsApplicationTheme(Application app) 
     { 
      return GetApplicationThemeUri(app) == ThemeResourceUri; 
     } 

     /// <summary> 
     /// Sets a value indicating whether this theme is the application theme. 
     /// </summary> 
     /// <param name="app">Application instance.</param> 
     /// <param name="value">True if this theme should be the application theme.</param> 
     public static void SetIsApplicationTheme(Application app, bool value) 
     { 
      SetApplicationThemeUri(app, ThemeResourceUri); 
     } 
    } 

然後,您只需創建一個資源字典並將其命名爲Theme.xaml,並將所有樣式放入其中。

<ResourceDictionary 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 

<Style TargetType="Border"> 
    <Setter Property="VerticalAlignment" Value="Top"/>  
    <Setter Property="HorizontalAlignment" Value="Stretch"/>  
    <Setter Property="Background" Value="{StaticResource TitleBackground}"/> 
    <Setter Property="Padding" Value="25,0"/> 
</Style> 

<Style TargetType="TextBlock"> 
    <Setter Property="VerticalAlignment" Value="Center"/>  
    <Setter Property="TextAlignment" Value="Center"/>  
    <Setter Property="FontSize" Value="48"/>  
    <Setter Property="Foreground" Value="{StaticResource TextForeground}"/> 
</Style> 
</ResourceDictionary> 

最後,用它包裹你的邊框!

<local:BorderTheme> 
     <Border> 
      <TextBlock Text="TextBlock"/> 
     </Border> 
    </local:BorderTheme> 

就是這樣。您應該能夠看到應用於邊框的樣式以及TextBlock。 :)

+0

我很困惑。這是如何幫助我的情況。我想要製作一個時鐘,並且你給我一張火箭飛船的藍色版畫。 – Jordan 2011-04-28 13:27:08

+0

嗨,我已經更新了我的答案。讓我知道它是否有效。 :) – 2011-04-28 23:29:44

相關問題