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

6

我正在玩弄Silverlight,并尝试设置一个样式以应用于所有TextBlock。以下是XAML:

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

我收到了一个错误消息:属性TargetType的值{x:Type TextBlock}无效。

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

以下是它的外观:

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


你想把这个样式放在哪里?是在 Window.Resources 中吗? - Quintin Robinson
你想让这种样式级联到用户控件元素内的所有文本块吗? - Quintin Robinson
6个回答

6
Silverlight不支持通过通用样式(即具有TargetType但没有静态资源键 - x:Key =“”)进行隐式样式处理,但WPF可以。
您需要使用每个要使用Style =“{StaticResource stylename}”进行风格设置的元素的StaticResource引用显式应用样式。
{{link1:Silverlight toolkit}}具有隐式样式管理器(ISM),它通过包装Silverlight标记并解析内容来应用来自ResourceDictionaries的样式。

4

将TargetType的值仅更改为TextBlock。它应该可以工作。

<Style TargetType="TextBlock">
   <Setter Property="Margin" Value="10, 10, 10, 10" />
</Style>

如果需要的话,给它一个x:Key属性,然后在你的TextBlock中使用StaticResource来引用这个属性的值。

<Style x:Key="someStyleName" TargetType="TextBlock">
   <Setter Property="Margin" Value="10, 10, 10, 10" />
</Style>
...
<TextBlock x:Name="myTextBlock" Text="Silverlight" Style="{StaticResource someStyleName}"/> 

1
当我这样做时,我没有收到错误提示,但是样式并未应用于用户控件中的任何文本块。 - Spencer Ruport
1
给它一个x:Key属性,并在TextBlock控件中使用它。我在我的答案中添加了示例代码。 - CZFox
1
是的,那个方法可行,但我宁愿不用那种方式。 :( - Spencer Ruport
仍然不确定问题出在哪里。噢,好吧。 - Spencer Ruport

4

由于您所要做的是隐式样式,因此戈登的答案似乎是正确的:“Silverlight不支持通过通用样式进行隐式样式(即具有TargetType但没有静态资源键 - x:Key =“”),但WPF支持。”

然而,在Silverlight 4中,隐式样式将会起作用。请参阅http://www.silverlightshow.net/items/Implicit-Styles-in-Silverlight-4.aspx


2
嗯,以下内容应该可行,并且会级联到用户控件元素中的所有文本块。
<UserControl>
    <UserControl.Resources>
        <Style TargetType="TextBlock">
            <Setter Property="Margin" Value="10" />
        </Style>
    </UserControl.Resources>
    <TextBlock Text="This has a margin of 10 on all sides!" />
</UserControl>

编辑:
NIRC.Page是用户控件的正确代码后台吗?

我希望我知道哪里出了问题,以下内容在用户控件中对我完美运行。

<UserControl x:Class="..."
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="300" Width="300">
    <UserControl.Resources>
        <Style TargetType="TextBlock">
            <Setter Property="Margin" Value="10" />
            <Setter Property="Foreground" Value="Red" />
        </Style>
    </UserControl.Resources>
    <TextBlock>Hello World!</TextBlock>
</UserControl>

结果是红色文本,在所有边缘上具有10像素的边距。


根据此回复,我编辑了我的帖子。 - Spencer Ruport

2
是的,Silverlight 4 现在支持隐式样式了。只需像 Quinton 所说的那样设置 TargetType 而不需要键,就可以轻松实现。将其放置在 App.xaml 中,它应该会传播到应用程序中的所有控件。

0

如果您不想每次使用控件时都设置Style,可以在构造函数代码中设置:

Style = (Style)Application.Current.Resources["YourStyle"];

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接