Posted on July 2, 2009 - by Adam Elmore
Custom Control Resources
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

generic.xaml

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

TabControl2.cs

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
). 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

TabControl2.cs

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

Visit My Website
July 2, 2010
Permalink
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….
Visit My Website
July 15, 2010
Permalink
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….
Visit My Website
August 30, 2010
Permalink
window http://umotorolaaag9x.ABABYCLOTHES.INFO/tag/price+window+Units+Carrier/ : Units…
Carrier…