Nick Heiner
Nick Heiner

Reputation: 122490

Silverlight: multiple values for TargetType?

I can define a style as follows in Silverlight 4:

    <Style x:Name="Subtitle" TargetType="TextBlock">
        <Setter Property="Foreground" Value="#787878" />
        <Setter Property="FontWeight" Value="Light" />
     </Style>

However, I want to apply these properties to a Run as well. Can I have multiple values for the TargetType, or somehow have these styles propagate down the tree?

Upvotes: 2

Views: 872

Answers (2)

obenjiro
obenjiro

Reputation: 3750

Nope.. one Style - one TargetType...

Upvotes: 0

Josh
Josh

Reputation: 69262

Normally you can create a style that targets a common base class and then create empty styles that derive from the base style to target the specific classes. However, in the case of TextBlock and Run, they do not share a common base class and in fact, since Run doesn't derive from FrameworkElement, it doesn't even have a Style property.

However, if you're asking whether a Run will inherit the foreground/font properties of its parent TextBlock, then yes it will. But you won't be able to apply this style to a Run independently of its containing TextBlock.

Another option is to create static resources for your foreground brush and font weight like so:

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

<Grid.Resources>
  <FontWeight x:Key="SubtitleFontWeight">Light</FontWeight>
  <SolidColorBrush x:Key="SubtitleForeground" Color="#787878" />
</Grid.Resources>

  <TextBlock>
    <Run Text="Hello " />
    <Run Text="World!" 
         Foreground="{StaticResource SubtitleForeground}"
         FontWeight="{StaticResource SubtitleFontWeight}" />
  </TextBlock>

</Grid>

Upvotes: 4

Related Questions