How to reset an implicitly applied style in Silverlight

Posted : Wednesday, 28 December 2011 17:43:00

I was working on a RIA services application recently and came across a nasty little problem that wasted far more time than it should have (don’t they always) Confused smile

As of Silverlight 4 implicit styles are available whereby if you omit the Key attribute from a resource dictionary then any controls of that type which use the dictionary will automatically have that style applied. I had added a TextBlock to a UserControl and was expecting it to fill the grid cell in which it was placed but it consistently didn’t, I could have got round the problem by hardcoding widths into the control but to do so would violate the key principles of the XAML ethos. It was only by chance that I came across this in App.xaml

<Style TargetType="TextBox">

  <Setter Property="Width" Value="200"/>

  <Setter Property="Height" Value="25"/>

  <Setter Property="Margin" Value="5,5,5,5"/>

  <Setter Property="HorizontalAlignment" Value="Left"/>

  <Setter Property="VerticalAlignment" Value="Center"/>

  <Setter Property="TextAlignment" Value="Left"/>


The cause of the problem was an implicit style, I dug around a bit and found out that you can override any styles applied further up the control tree like so…

<TextBox Grid.Column="1" Grid.Row="1" Text="Something" Style="{x:Null}" ></TextBox>

The Style="{x:Null}" attribute is what resets the style.


I will remember to check any resource dictionaries next time this sort of thing happens…no really I will…

  • (This will not appear on the site)