silverlight - 将样式应用于 Silverlight 4 中的子元素

标签 silverlight styles

在 Silverlight 4(使用 Expression Blend 4)中,如何在包含 Border 的样式内更改 TextBox 的字体大小?我正在将样式从 WPF 转换为 Silverlight(总是很有趣)。这是我所拥有的:

<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 所建议的那样采用整个主题结构?)

最佳答案

实际上,您可以从 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>

就是这样。您应该能够看到应用于 Border 和 TextBlock 的样式。 :)

关于silverlight - 将样式应用于 Silverlight 4 中的子元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5794837/

相关文章:

html - 即使在一行文本中,格式也会发生很大变化,如何将我的文本格式移动到 CSS?

css - Div 透明度在 IE8 中不起作用

asp.net - Silverlight 或 ASP.NET

c# - 国际奥委会和银光

c# - wpf默认样式和样式基础

python - 在没有 NoneType 错误的情况下访问嵌套字典的 pythonic 方法是什么

html - 在 bootstrap v3.0.21 中我在哪里写我的响应样式

c# - Silverlight 和 WCF : NotFound error!

c# - 如何开发 Dicom 网页查看器

silverlight - 使用 StatLight 和 TeamCity 自动化 silverlight 单元测试