Countdown to Silverlight 3 #2: Setting styles

by Andrej Tozon 27. June 2009 14:53

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" />
 
Run the sample online

Source code below:

Tags:

Development | Silverlight

Comments

6/29/2009 10:04:58 PM #

Kirill Osenkov

I'd be curious if changing the style object will automatically update the appearance of all visuals that have this style applied? It'd rock if it'd be the case.

Kirill Osenkov United States | Reply

6/29/2009 10:16:15 PM #

Andrej

Kiril,
no, unfortunately this won't work in Silverlight. Styles can still be applied only as static resources. Dynamic resources (as seen in WPF) are still not supported.

Andrej Slovenia | Reply

8/28/2009 6:01:05 AM #

pingback

Pingback from topsy.com

Twitter Trackbacks for
        
        Andrej Tozon's blog | Countdown to Silverlight 3 #2: Setting styles
        [tozon.info]
        on Topsy.com

topsy.com | Reply

5/21/2010 1:15:45 AM #

pingback

Pingback from 220.mfbattle.com

F550 Super Duty Sell, F550 Discount

220.mfbattle.com | Reply

8/13/2010 2:19:15 PM #

pingback

Pingback from alabamaweddings.interactiveinfonet.info

Alabama weddings - Alabama reception - Alabama in package wedding

alabamaweddings.interactiveinfonet.info | Reply

Add comment




  Country flag

biuquote
  • Comment
  • Preview
Loading





Andrej Tozon
Andrej Tozon's on Twitter View Andrej Tozon's profile on LinkedIn Subscribe to me on FriendFeed Andrej Tozon's Facebook profile

MVP - Client Application Developer

Microsoft Certified Solution Developer

MSN Alerts

Get help from Andrej Tozon!

 

Currently reading


Microsoft Silverlight 4 Data and Services Cookbook

Stay tuned for the review... In the mean time, you can read an excerpt from the book.

RecentComments

Comment RSS