Silverlight:TargetType 的无效属性类型=“{x:Type TextBlock}"; [英] Silverlight: Invalid Attribute Type for TargetType="{x:Type TextBlock}"

查看:23
本文介绍了Silverlight:TargetType 的无效属性类型=“{x:Type TextBlock}";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是稍微玩弄 Silverlight 并尝试设置一种样式以应用于所有 TextBlock.以下 XAML:

Just playing around with Silverlight a bit and trying to set a style to apply to all TextBlocks. The following XAML:

<Style TargetType="{x:Type TextBlock}">
   <Setter Property="Margin" Value="10, 10, 10, 10" />
</Style>

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

我从 MSDN 复制并粘贴了这一点,所以我有点不明白为什么我会收到这个错误.

I copied and pasted this bit from the MSDN so I'm a little lost as to why I'm getting this error.

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

Here's the full code I'm attempting now:

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

外观如下:

替代文字 http://www.netortech.com/Content/slhw.jpg

推荐答案

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

Silverlight does not support implicit styling via generic Styles (i.e. with a TargetType but without a static resource key - x:Key="") but WPF does.

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

You need to explicitly apply Styles using StaticResource references on each instance of your element that you want styled using Style="{StaticResource stylename}".

Silverlight 工具包 有一个隐式样式管理器 (ISM),它通过包装 Silverlight 标记并应用通过解析内容从 ResourceDictionaries 中获取样式.

The Silverlight toolkit has an Implicit Style Manager (ISM) that gets around this by wrapping Silverlight markup and applying styles from ResourceDictionaries by parsing the content.

这篇关于Silverlight:TargetType 的无效属性类型=“{x:Type TextBlock}";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆