Andrej Tozon's blog

In the Attic

NAVIGATION - SEARCH

Countdown to Silverlight 3 #2: Setting styles

Silverlight 3 is introducing a small, but very powerful feature: styles in Silverlight 3 can be set multiple times! For example, executing the following code in Silverlight 2 will throw a “catastrophic failure” exception on the second line:

textBox.Style = Application.Current.Resources["TextBoxStyle"] as Style;
textBox.Style = Application.Current.Resources["TextBoxStyle"] as Style;

Silverlight 3, on the other hand, won’t have any problems with setting control’s style the second, third or n-th time, if required.

To reset a control’s style, one can simply set it to null:

textBox.Style = null;

… or assign an empty style to it:

<Style x:Key="EmptyTextBoxStyle" TargetType="TextBox" />
 

Source code below: