【问题标题】:Can UserControls have implicit styles in Silverlight?UserControls 可以在 Silverlight 中具有隐式样式吗?
【发布时间】:2011-05-31 21:51:56
【问题描述】:

解决方案:UserControl 不是实际类型,所以我不能在用户控件上使用隐式样式。谢谢蒂姆。

以下隐式样式似乎没有任何作用。

<Style TargetType="UserControl">
    <Setter Property="FontFamily" Value="Webdings"/>
    <Setter Property="Foreground" Value="Red"/>
</Style>

我知道样式正在被加载到 Apps 资源字典中,因为如果我在同一个 .xaml 文件中明确设置另一种样式,它将正常工作。

例子:

<Style TargetType="Control" x:Key="BaseStyle">
    <Setter Property="FontFamily" Value="Webdings"/>
</Style>

如果我将 Style="{StaticResource BaseStyle}" 放在标签中,效果很好。 谢谢 -肖恩

【问题讨论】:

  • 如果您指定您的实际类型而不是 UserControl 作为 TargetType,它是否有效?
  • 这是我的问题。隐式仅适用于特定类型,并且由于我的所有用户控件都继承自它,因此我不能使用隐式样式。谢谢。
  • @Tim - 您应该将您的评论推广到答案(即使它很短,也许可以再详细一点),以便@shane 可以将其标记为答案。
  • @slugster - 好电话。我现在已经做到了。

标签: c# silverlight xaml


【解决方案1】:

您需要指定控件的实际类型,而不仅仅是使用 UserControl。隐式样式仅适用于特定类型。所以而不是:

<Style TargetType="UserControl">    
    <Setter Property="FontFamily" Value="Webdings"/>    
    <Setter Property="Foreground" Value="Red"/>
</Style>

你会使用:

<Style TargetType="my:MyUserControl">    
    <Setter Property="FontFamily" Value="Webdings"/>    
    <Setter Property="Foreground" Value="Red"/>
</Style>

其中my 被声明为您的命名空间,MyUserControl 是您的 UserControl 派生控件的实际类名。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-05
    • 1970-01-01
    • 2011-10-30
    • 1970-01-01
    • 2011-03-16
    • 1970-01-01
    • 2012-03-03
    • 1970-01-01
    相关资源
    最近更新 更多