Tips when using Styles in WP7
published on: 1/7/2011 | Views: N/A | Tags: Styling UI BestPractices
by WindowsPhoneGeek
A Style is basically a collection of property settings applied to multiple instances of the same type. Style mechanism in Silverlight for Windows Phone 7 allows us to encapsulate control property values as a reusable resource.We can store Styles in separate files from our page or we can place them in the Resources section of the page.
NOTE:You can set a Style on any element that derives from FrameworkElement through its Style property. Style is most commonly declared with the x:Key attribute as a resource inside the Resources section in a XAML file and then referenced as a StaticResource.
NOTE: When creating a Style with ExpressionBlend (Edit Style –> Create Empty) it automatically add the style into the App.xaml Resources section as an external resource dictionary.
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionarySource="TestStyles.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
You can examine the style itself in TestStyles.xaml,
<Stylex:Key="TextBlockStyle1"TargetType="TextBlock">
<Setter Property="Foreground" Value="Orange"/>
<Setter Property="FontSize" Value="24"/>
<Setter Property="VerticalAlignment" Value="Bottom"/>
</Style>
NOTE:In addition to defining basic property settings (Color, Font, Size, Margins, etc), styles in Silverlight can also be used to define and re-use Control Templates.For more information about the ControlTemplate take a loot the the previous post: Working with ControlTemplates in Silverlight for WP7.
NOTE:If there is more than one setter in the setter collection with the same Property value, the setter that is declared last is used. Similarly, if you set a value for the same property in a style and on an element directly, the value set on the element directly takes precedence.
NOTE:You must set the TargetType property when you create a Style. If you do not, an exception is thrown.
Example:
<Grid> <Grid.Resources>
<Style TargetType="TextBlock" x:Key="TextBlockStyle1">
<Setter Property="Foreground" Value="Orange"/>
<Setter Property="FontSize" Value="24"/>
<Setter Property="VerticalAlignment" Value="Bottom"/><Setter Property="Template">
….
</Setter>
</Style></Grid> </Grid>
...<TextBlock Style="{StaticResource TextBlockStyle1}"/>
Reference: Style Class on MSDN
You can also follow us on Twitter @winphonegeek
Comments
Our Top Articles & Free books
- Our FREE e-book: "Windows Phone Toolkit In Depth" 2nd edition
- 400+ Windows Phone Development articles in our Article Index
- 21 WP7 Toolkit in Depth articles covering all controls
- 12 WP7 Coding4Fun Toolkit in Depth articles covering all controls
- Performance Tips when creating WP7 apps
- Creating a WP7 Custom Control in 7 Steps
- WP7 working with VisualStates: How to make a ToggleSwitch from CheckBox
- What makes a WP7 App successful
- Creating theme friendly UI in WP7 using OpacityMask
- Implementing Windows Phone 7 DataTemplateSelector and CustomDataTemplateSelector
- All about Splash Screens in WP7 – Creating animated Splash Screen
- Getting Started with Unit Testing in Silverlight for WP7
- WP7 WatermarkedTextBox custom control
Our Top Tips & Samples
- All about WP7 Isolated Storage series
- WP7 Dynamically Generating DataTemplate in code
- 5 tips for a successful WP7 Marketplace submission
- WP7: Navigating to a page in different assembly
- WP7 ContextMenu: answers to popular questions
- WP7 ListBox: answers to popular questions
- WP7 working with Images: Content vs Resource build action
- WP7 Element Binding samples
- WP7 working with XML: reading, filtering and databinding
- Drawing in WP7: #2 Drawing shapes with finger
- WP7 TextBox Light theme problems - the solution
- Changing the WP7 Panorama Background Image dynamically with Animation
