Friday, October 12, 2012

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

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

Answers & Comments...

Answer: 1

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}"/> 
by : CZFoxhttp://stackoverflow.com/users/79481

Answer: 2

Hmm, the following should work and cascade to all textblocks 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 the correct codebehind for the usercontrol?

I wish I knew what was wrong, the following works perfect for me in a user control.

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

Result is red text with a margin of 10px on all sides.

by : Quintin Robinsonhttp://stackoverflow.com/users/12707

Answer: 3

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.

by : Gordon Mackie JoanMirohttp://stackoverflow.com/users/15778

Answer: 4

Since what you are trying to do is implicit styling, so far Gordon's answer seems the right one: "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."

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

by : Denis Dollfushttp://stackoverflow.com/users/156959

Answer: 5

Yeah, Silverlight 4 lets you do implicit styles now, you just need to do what Quinton says and just set the TargetType without a key, and you're good to go. Put it in the App.xaml and it should propogate the style to all controls in the app.

by : TrueHarlequinhttp://stackoverflow.com/users/466175

Answer: 6

If you don't want to set Style each time you use your control, you can set it in your constructor code:

Style = (Style)Application.Current.Resources["YourStyle"]; 
by : lgm42http://stackoverflow.com/users/856355




No comments:

Post a Comment

Send us your comment related to the topic mentioned on the blog