WPF style and behavior, WPF style Behavior
Different styles and templates in WPF
Simply put, if you only need to slightly modify the control (adjust the size, position, Font, color, etc.), you can use the style, if you need to change the appearance and behavior of the control, use controlTemplate (shape, event triggering, such as mouse stop effect, etc ). In actual projects, we often define templates in styles by setting styles.
For example, if I want to create an image button for scaling a map, adjust the position using the style and use controltemplate to set the button shape.
<UserControl. Resources>
<Style x: Key = "ZoomUpButton" TargetType = "Button">
<Setter Property = "Width" Value = "23.5"/>
<Setter Property = "Height" Value = "25.5"/>
<Setter Property = "Template">
<Setter. Value>
<ControlTemplate TargetType = "Button">
<Grid Width = "23.5" Height = "25.5">
<Image Source = "/Resource/zoom=.png" Stretch = "Fill"/>
<ContentPresenter HorizontalAlignment = "Center"/>
</Grid>
<ControlTemplate. Triggers>
<Trigger Property = "IsMouseOver" Value = "True">
<Setter Property = "Effect">
<Setter. Value>
<DropShadowEffect ShadowDepth = "2"/>
</Setter. Value>
</Setter>
... The remaining full text>
WPF style problems
Is the original template changed in the style of this button? If you change the content, you need a ContentPresenter to define the content. Otherwise, you cannot set the content. The following is an example.
<Style TargetType = "Button">
<Setter Property = "Template">
<Setter. Value>
<ControlTemplate TargetType = "Button">
<Border x: Name = "back">
<ContentPresenter x: Name = "content" HorizontalAlignment = "Center" verticalignment = "Center" Content = "{TemplateBinding Content}">
</ContentPresenter>
</Border>
</ControlTemplate>
</Setter. Value>
</Setter>
</Style>