开发者

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?


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>


Nope.. one Style - one TargetType...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜