Perhaps you want your two ‘TextBlock’ elements to share some property values such as the FontFamily and the centered HorizontalAlignment, but you also want the text ‘My Pictures’ to have some additional properties. You can do that by creating a new style that is based on the first style, as shown here.
[XAML]
<Window.Resources>
...
<!--A Style that extends the previous MyBaseStyle Style--><!--This is a 'named style' with an x:Key of MyDerivedStyle--><StyleBasedOn='{StaticResource {x:Type MyBaseStyle}}'TargetType='TextBlock'x:Key='MyDerivedStyle'><SetterProperty='FontSize'Value='26'/><SetterProperty='Foreground'><Setter.Value><LinearGradientBrushStartPoint='0.5,0'EndPoint='0.5,1'><LinearGradientBrush.GradientStops><GradientStopOffset='0.0'Color='#90DDDD' /><GradientStopOffset='1.0'Color='#5BFFFF' /></LinearGradientBrush.GradientStops></LinearGradientBrush></Setter.Value></Setter></Style>
Here is a very good tool put togather by Chris Sells and Ian Griffiths that shows the templates corresponding to each control in the Presentation Framework dll:
In Extensible Application Markup Language (XAML), style and template properties can technically be set in any one of two ways provided. You can use ’attribute syntax’ to reference a style that was defined within a resource, for example <object Style='{StaticResource myResourceKey}’ …/>. Or you can use ’property element syntax’ to define an inline style for an instance.
The attribute usage is much more common. A style that is defined inline and not defined in resources is necessarily scoped to the containing element only and cannot be re-used easily because it has no resource key. In general a resource-defined style is more versatile and useful in keeping with the general Windows Presentation Foundation (WPF) programming model principle of separating program logic in code from design in markup.
Usually there is no reason to set a style or template inline even if you intend to use that style or template in that location only. Most elements that can take a style or template also support a ’content property’ and a ’content model’. If you only use the logical tree you create through styling or templating once, it would be even easier to just fill that content property with the equivalent child elements in direct markup. This would bypass the style and template mechanisms altogether.
Other syntaxes enabled by markup extensions that return an object are also possible with styles and templates. Two such extensions that have possible scenarios include ’TemplateBinding’ and ’Binding’.
The WPF theme engine will switch styles automatically for you if you place them in separate ’ResourceDictionary’ XAML files compiled in your application.
The files need to be named.
themes\..xaml
For example, these are the themes that Microsoft produces.
Windows Presentation Foundation does not provide an event to notify the user regarding theme changes. If you need to go beyond what is provided by styles in a ResourceDictionary, you will need to listen to the ’WM_THEMECHANGE’ message in a window hook.
The ControlTemplate specifies the appearance of a Control. If a Control does not have a ControlTemplate, the Control will not appear in your application. The control author defines the default control template and the application author can override the ControlTemplate to redefine the visual tree of the control. A ControlTemplate is intended to be a self-contained unit of implementation detail that is invisible to outside users and objects, including Style objects. The content of the ControlTemplate can be manipulated within the same ControlTemplate only.
The following example creates a ControlTemplate for a Button. If you add this to your application as a resource, all the buttons in the application will appear as ellipses but will still function as buttons.
[XAML]
<StyleTargetType='Button'><!--Set to true to not get any properties from the themes.--><SetterProperty='OverridesDefaultStyle'Value='True'/><SetterProperty='Template'><Setter.Value><ControlTemplateTargetType='Button'><Grid><EllipseFill='{TemplateBinding Background}'/><ContentPresenterHorizontalAlignment='Center'VerticalAlignment='Center'/></Grid></ControlTemplate></Setter.Value></Setter></Style>