• Home
  • About
  • Archives
Subscribe: Posts | Comments | E-mail
  • .Net
  • Silverlight

Silverlight Muffins

Posted on July 2, 2009 - by Adam Elmore

Custom Control Resources

Silverlight

This morning I ran into an interesting bug while working on a custom TabControl created by another member of my team. This TabControl (appropriately named, “TabControl2″) adds a button in the TabPanel for adding new TabItems (among other things). For various reasons, this Button is created dynamically in the class definition and added to the current TabPanel. No problems here until it was time to style the Button in a way that would fit the application. After taking a look at TabControl2.cs, I quickly saw the first mistake:

TabControl2.cs

OnApplyTemplate

generic.xaml

2-6-2009_6

AddTabButtonStyle is a DependencyProperty used to update the Style of the dynamically created Button. As you can see, an attempt is made to set the value of this property to the “Local_NewTabButtonStyle” Style. Knowing that the Style defined in generic.xaml is not located within this control’s Resources, I saw a few possible solutions. The obvious solution was to store the Style somewhere within the TabControl2 ControlTemplate. This would give us easy access to the resource in the TabControl2 OnApplyTemplate() method. However, this particular control didn’t have a ControlTemplate defined as it derived directly from TabControl and did not make any changes that warranted a new template (outside of this particular Button). At this point, the smart move would have been to create a default ControlTemplate targeting the TabControl2 control and throw my Button Style directly into the template. Another solution involved creating the Style in C# (no thanks). Turns out, I’m lazy and went with solution number three: move the Local_NewTabButtonStyle resource to App.xaml (that’s right, the application level resources; not so “Local” anymore..), and pull it from Application.Current.Resources.

App.xaml

2-6-2009_3

TabControl2.cs

2-6-2009_4

Aside from the fact that this is very poor design when it comes to creating a custom control, using this “solution” also introduced a very strange bug. I initially ran the application and saw that the style was being applied correctly (after all, the style does reside within the application’s ResourceDictionary). Not feeling the least bit guilty, I moved on to more important tasks (don’t judge me :P ). An hour later, our team designer hopped on his Mac to take a look at the changes only to find that the application wouldn’t load. I picked up my wife’s MacBook and confirmed the problem: after finishing the Silverlight loading animation, the application loaded a blank white screen. In addition, the browser’s error console was void of any Silverlight exceptions (this problem was occurring in both Firefox and Safari). Strange.

After much toil, I came to the realization that my Application level resource was the culprit. After creating a ControlTemplate for TabControl2 and storing the Style in the root Grid’s resources, I quickly remedied the problem.

generic.xaml

2-6-2009_2

TabControl2.cs

2-6-2009_5

I’d love to hear an explanation for this issue. One thing is for sure – it never rarely pays to take the quick route.

This entry was posted on Thursday, July 2nd, 2009 at 10:53 pm and is filed under Silverlight. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

3 Comments

We'd love to hear yours!



  1. Visit My Website

    July 2, 2010

    Permalink

    DUANE said:


    Pillspot.org. Canadian Health&Care.Best quality drugs.No prescription online pharmacy.Special Internet Prices. Low price drugs. Buy drugs online…

    Buy:Human Growth Hormone.Zovirax.Synthroid.Nexium.Prevacid.Arimidex.Accutane.100% Pure Okinawan Coral Calcium.Zyban.Lumigan.Valtrex.Prednisolone.Actos.Petcam (Metacam) Oral Suspension.Retin-A.Mega Hoodia….



  2. Visit My Website

    July 15, 2010

    Permalink

    RAFAEL said:


    Pillspot.org. Canadian Health&Care.Best quality drugs.No prescription online pharmacy.Special Internet Prices. Low price drugs. Order pills online…

    Buy:Buspar.Seroquel.Female Pink Viagra.SleepWell.Benicar.Amoxicillin.Wellbutrin SR.Ventolin.Zocor.Lipitor.Lipothin.Aricept.Nymphomax.Female Cialis.Zetia.Advair.Prozac.Lasix.Acomplia.Cozaar….



  3. Visit My Website

    August 30, 2010

    Permalink

    Carrier said:

    window http://umotorolaaag9x.ABABYCLOTHES.INFO/tag/price+window+Units+Carrier/ : Units…

    Carrier…



Leave a Comment

Here's your chance to speak.

  1. Name (required)

    Mail (required)

    Website

    Message

  • Recent Posts

    • Forward Time Travel & MSDN Roadshow Events
    • Silverlight 3 Bits
    • Silverlight – My Favorite “Web Design” Technology
    • Custom Control Resources
  • Archives

    • August 2009
    • July 2009
  • Blogroll

    • Dino Esposito
    • Jesse Liberty
    • John Papa
    • Laurence Moroney
    • Mike Snow
    • Scott Guthrie
    • Tim Heuer
    • Tim Sneath
© 2008 Silverlight Muffins - Sharing with the world, one muffin at a time.
jeux de casino by jeux de casino