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

Add comment




  Country flag

biuquote
  • Comment
  • Preview
Loading





Andrej Tozon

MVP - Client Application Developer

Microsoft Certified Solution Developer

MSN Alerts

View Andrej Tozon's profile on LinkedIn

Subscribe to me on FriendFeed

Andrej Tozon's Facebook profile

Get help from Andrej Tozon!

RecentComments

Comment RSS