Adam Calderon – C# MVP Application Development Practice Lead Interknowlogy
More info on InterKnowlogy: Contact Information Phone: x274 Blog: About Adam Calderon Microsoft MVP – C# Microsoft UI Server Frameworks Advisory Council Developer / Author / Speaker / Teacher
What Styles are and how to use them How Triggers can be used to replace common UI behavior How Templates can be used to enhance the look of a control
Styles Triggers Templates
What are styles? Problems they address in WinForms 2.0 Styling controls one at a time Unnecessary Custom Control Creation Similarities to Cascading Style Sheets Inline declaration Page level declaration Separate file declaration
Contained in System.Windows.Style Main Properties Key TargetType Setters ▪ Property – Can set element properties ▪ Event – Can set names to common event handlers
Some Text
Static Resources Loaded when Window or Page Loads Leads to slower page loading Changes to underlining values are not propagated Dynamic Resources More overhead to allow for constant updates Can only be used to set dependency properties Can potentially improve Page and Window load time
Contains hash table of resources Great way to organize resources Resource.MergedDictionaries combines many different file based resources
Application Level Application.Resources Page/Window Level Window.Resources Page.Resources Element Level Grid.Resources
Order of Precedence Application Level ▪ Page/Window Level (Overrides Application) ▪ Element Level (Final Override) BasedOn Property Style-based Element-based
Organize styles into scope areas Application Page Element Use Resource Dictionaries Use inheritance (BasedOn) in cases where you want to override a style for a particular application area Try to limit element use of element based styles
Styles in Action
Styles Triggers Templates
Setters that are set based on one or more conditions Three Types of Triggers Property – Invoked when the value of a dependency property changes Data – Invoked when the value of a plain.NET property changes Event – Invoked when a routed event changes
Based on System.Windows.Trigger Set when a specified property has a specified value Reset when the property no longer has this value
Set when a specified property (.NET based or dependency) has a specified value Reset when the property no longer has this value <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self},Path=Text}" Value="Color">
Triggers can be logically Or’d together to solve unique problems If both conditions are true the last one wins Last one Wins when both are true
Triggers can be logically AND’d together to solve unique problems Implemented using MultiTrigger
Triggers in Action
Styles Triggers Templates
Pick up where properties leave off Provide ability to completely replace a controls visual tree Not about replacing functionality Reinforces separation of functionality and view Out-of-box controls get their visual appearance from Templates Brings together layout, styles and triggers
Three types of templates: ControlTemplate ItemsPanelTemplate DataTemplate
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/> <Button Height="20" Width="40" Background=“Blue” Template="{StaticResource ButtonTemplate}">Ok
Based on System.Windows.Controls.ControlTemplate Resources TargetType Trigggers TemplateBinding links template and control properties ContentPresenter can be used to display controls content
Markup extension similar to binding More lightweight than binding and designed for templates Data source is always the target control
Provides ability to show all types of content Preferred way to provide ability for your template to show content Remember that your are replacing the visual representation of a control and correctly showing contained content is important
Templates in Action
Styles are powerful and flexible Triggers eliminate user interaction-based UI coding Controls are more about behavior Control Templates modify visual aspects of controls
MSDN ® Links Microsoft Windows ® Vista ™ development center: us/windowsvista/default.aspx us/windowsvista/default.aspx Microsoft.NET Framework 3.0 for developers: Other Links Microsoft.NET Framework:
More info on InterKnowlogy: Contact Information Phone: x274 Blog: About Adam Calderon Microsoft MVP – C# Microsoft UI Server Frameworks Advisory Council Developer / Author / Speaker / Teacher