Style
A Style is a group of Setter and Trigger actions that affect properties of the styled object.
A Setter is an action that sets a specified property to the specifed value. A Style possess a collection of Setter actions which are automatically applied (called Setters).
A Trigger is an action which, as its name indicates, is triggerred by a particular condition. Depending on the Trigger type, a Trigger can contain Setters or Storyboards. A Style possesses a collection of Trigger actions, rightfully called Triggers.
<Style x:Key="DialogButtonStyle" TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid>
<Ellipse Fill="{TemplateBinding Background}"
Stroke="{TemplateBinding BorderBrush}"/>
<ContentPresenter HorizontalAlignment="Center"
VerticalAlignment="Center"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Button Style="{StaticResource DialogButtonStyle}" />
Style vs. Template: What is the difference?
Style is used to set the properties of an object to particular values. In addition, a Style allows Triggers based on styled object events, styled object property values, and data values. In turn, triggers allow the animation of the object's properties.
In contrast, a Template is used to define the Visual Tree of a Control (ControlTemplate) or the Visual Tree of the representation of an object (DataTemplate). While templates also have Triggers, it is to be noted that Style triggers cannot interact with particular items of the Visual Tree for the object.
One common distinction of Styles and Templates states that a Style can contain one/many Setters/Triggers to change the ControlTemplate or DataTemplate of controls/objects.
No comments:
Post a Comment