Silverlight: invalid attribute type for TargetType = "{x: Type TextBlock}"

Just play around with Silverlight a bit and try to style all text blocks. Next 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 MSDN, so I lost a bit why I get this error.

EDIT:

Here is the complete code I'm trying 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 what it looks like:

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

+6
silverlight xaml
source share
6 answers

Silverlight does not support implicit style using common styles (i.e. with TargetType, but without a static resource key - x: Key = ""), but WPF does.

You need to explicitly apply styles using the StaticResource links for each instance of your element that you want to create using Style = "{StaticResource stylename}".

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

+6
source share

TargetType only changes to TextBlock. It should work.

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

Optionally, give it x: The key and value of this attribute is used in your TextBlock as a 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}"/> 
+4
source share

Since what you are trying to do is an implicit style, so far Gordon's answer seems to be correct: "Silverlight does not support implicit styling using common styles (i.e. with TargetType, but without a static resource key - x: Key =" ), but WPF does. "

However, implicit styles will work with Silverlight 4 . See http://www.silverlightshow.net/items/Implicit-Styles-in-Silverlight-4.aspx

+4
source share

Hmm, the following should work and cascade to all text blocks in the usercontrol element.

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

Edit:
Is NIRC.Page correct code for usercontrol?

I wish I knew what was wrong, the following works ideally for me in user management.

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

The result is red text with an edge of 10 pixels on all sides.

+2
source share

Yes, Silverlight 4 allows you to do implicit styles now, you just need to do what Quinton says, and just install TargetType without a key, and you're good to go. Put it in App.xaml and it should extend the style to all controls in the application.

+2
source share

If you do not want to install Style every time you use your control, you can set it in your constructor code:

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

All Articles