.net - Silverlight:TargetType 的多个值?

标签 .net silverlight xaml targettype

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

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

但是,我也想将这些属性应用于 Run。我可以为 TargetType 设置多个值,或者以某种方式让这些样式沿着树传播吗?

最佳答案

通常,您可以创建一个针对公共(public)基类的样式,然后创建从基样式派生的空样式以针对特定的类。但是,对于 TextBlock 和 Run,它们不共享公共(public)基类,事实上,由于 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>

关于.net - Silverlight:TargetType 的多个值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4394351/

相关文章:

Silverlight 文件类型关联

c# - 为自定义 WCF 绑定(bind)指定 HTTPS

c# - 将样式的 TargetType 属性设置为通用类

c# - WPF 项目控制 : Can't seem to get items to display in the view

.net - 正则表达式从 URL 获取文件夹名称

.net - Entity Framework + 存储库 + 单元或工作问题

.net - 如何将命令行参数传递给已经运行的进程?

silverlight - 我可以将 dll 添加到 Silverlight 3 应用程序吗?

C# 映射两个复杂对象

c# - 操纵事件和平移模式