【问题标题】:Silverlight: Invalid Attribute Type for TargetType="{x:Type TextBlock}"Silverlight:TargetType="{x:Type TextBlock}" 的属性类型无效
【发布时间】:2009-03-20 18:10:51
【问题描述】:

只是稍微玩弄一下 Silverlight 并尝试设置一种样式以应用于所有 TextBlock。以下 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 复制并粘贴了这一点,所以我有点不知道为什么会出现这个错误。

编辑:

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

<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 中?
  • 所以您希望这种样式级联到用户控件元素中的所有文本块?

标签: .net silverlight xaml


【解决方案1】:

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

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

Silverlight toolkit 有一个隐式样式管理器 (ISM),它通过包装 Silverlight 标记并通过解析内容应用来自 ResourceDictionaries 的样式来解决此问题。

【讨论】:

    【解决方案2】:

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

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

    可选地,给它 x:Key 和这个属性的值在你的 TextBlock 中用作静态资源。

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

    【讨论】:

    • 当我这样做时,我没有收到错误,但样式并未应用于用户控件中的任何文本块。
    • 给它 x:Key 属性并在你的 TextBlock 控件中使用它。我在答案中添加了示例代码。
    • 是的,这行得通,但我宁愿不必那样做。 :(
    • 仍然不确定是什么问题。哦,好吧。
    【解决方案3】:

    由于您尝试做的是隐式样式,因此到目前为止,Gordon 的答案似乎是正确的:“Silverlight 不支持通过通用样式进行隐式样式(即使用 TargetType 但没有静态资源键 - x:Key=" ") 但 WPF 可以。"

    但是隐式样式将适用于 Silverlight 4。见http://www.silverlightshow.net/items/Implicit-Styles-in-Silverlight-4.aspx

    【讨论】:

      【解决方案4】:

      嗯,以下应该可以工作并级联到用户控件元素中的所有文本块。

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

      结果是四边距为 10px 的红色文本。

      【讨论】:

      • 针对此问题编辑了我的帖子。
      【解决方案5】:

      是的,Silverlight 4 现在让您可以使用隐式样式,您只需要按照 Quinton 所说的进行操作,只需设置 TargetType 而无需使用键,就可以开始使用了。将它放在 App.xaml 中,它应该将样式传播到应用程序中的所有控件。

      【讨论】:

        【解决方案6】:

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-10-28
          • 1970-01-01
          • 1970-01-01
          • 2010-12-10
          • 1970-01-01
          • 2022-07-14
          • 1970-01-01
          相关资源
          最近更新 更多