Применение стиля к дочерним элементам в Silverlight 4 - PullRequest
3 голосов
/ 26 апреля 2011

В Silverlight 4 (используя Expression Blend 4), как я могу изменить размер шрифта TextBox в стиле Border, в котором он находится? Я конвертирую стиль из 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 (без использования всей тематической конструкции, как предлагает Синь?)

1 Ответ

0 голосов
/ 28 апреля 2011

На самом деле вы можете получить то, что вы хотите, из тем набора инструментов Silverlight.Вы можете найти его здесь (Theming -> Theme Browser).

Обновление:

Сначала необходимо создать класс, который наследуется отТема (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>

Вот и все.Вы должны увидеть стили, примененные к вашей границе, а также к вашему текстовому блоку.:)

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...