Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Silverlight: Invalid Attribute Type for TargetType="{x:Type TextBlock}"

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>

Gives me the error Invalid attribute value {x:Type TextBlock} for property TargetType.

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

EDIT:

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>

Here's how it looks:

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

like image 522
Spencer Ruport Avatar asked Mar 20 '09 18:03

Spencer Ruport


2 Answers

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.

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

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.

like image 168
Gordon Mackie JoanMiro Avatar answered Sep 23 '22 12:09

Gordon Mackie JoanMiro


Value of TargetType change to TextBlock only. It should work.

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

Optionally, give it x:Key and the value of this attribute use in your TextBlock as 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}"/> 
like image 37
CZFox Avatar answered Sep 22 '22 12:09

CZFox