Silverlight:TargetType 的多个值?

发布于 2024-10-06 22:10:18 字数 359 浏览 2 评论 0原文

我可以在 Silverlight 4 中定义如下样式:

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

但是,我也想将这些属性应用到 Run 。我可以为 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

偏爱自由 2024-10-13 22:10:18

通常,您可以创建一个针对公共基类的样式,然后创建从基样式派生的空样式以针对特定的类。但是,对于 TextBlock 和 Run,它们不共享公共基类,事实上,由于 Run 不是从 FrameworkElement 派生的,因此它甚至没有 Style 属性。

但是,如果您询问 Run 是否会继承其父 TextBlock 的前景/字体属性,那么是的。但是您将无法将此样式应用于独立于其包含的 TextBlock 的 Run。

另一种选择是为前景画笔和字体粗细创建静态资源,如下所示:

<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>

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>
↙温凉少女 2024-10-13 22:10:18

不..一种样式 - 一种目标类型...

Nope.. one Style - one TargetType...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文