Silverlight:TargetType的属性类型无效="{x:Type TextBlock}"

Spe*_*ort 6 .net silverlight xaml

只是稍微使用Silverlight并尝试设置一个样式以应用于所有TextBlocks.以下XAML:

<Style TargetType="{x:Type TextBlock}">
   <Setter Property="Margin" Value="10, 10, 10, 10" />
</Style>
Run Code Online (Sandbox Code Playgroud)

给我错误 Invalid attribute value {x:Type TextBlock} for property TargetType.

我从MSDN复制并粘贴了这一位,所以我有点迷失为什么我收到这个错误.

编辑:

这是我现在正在尝试的完整代码:

<UserControl x:Class="NIRC.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Width="400" Height="300">
    <UserControl.Resources>
        <Style TargetType="TextBlock">
            <Setter Property="Margin" Value="10" />
            <Setter Property="Foreground" Value="Red" />
        </Style>
    </UserControl.Resources>
    <TextBlock>Hello World!</TextBlock>
</UserControl>
Run Code Online (Sandbox Code Playgroud)

以下是它的外观:

alt text http://www.netortech.com/Content/slhw.jpg

Gor*_*iro 6

Silverlight不支持通过通用样式的隐式样式(即使用TargetType但没有静态资源键 - x:Key ="")但WPF却支持.

您需要在元素的每个实例上使用Style ="{StaticResource stylename }" 使用StaticResource引用显式应用样式.

Silverlight工具包有通过包装Silverlight的标记,并通过解析内容应用从ResourceDictionaries样式解决此得到一个隐式样式管理器(ISM).