控件:
<Button Cursor="Hand" x:Name="btn_Return" Click="btn_Return_Click" Style="{DynamicResource setting}"/>
样式:
<Style x:Key="setting" TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid Background="Transparent">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Image Source="Image/return.png" Stretch="None" VerticalAlignment="Center" Grid.Column="0" Margin="5,0,5,0"/>
<TextBlock Text="{TemplateBinding Content}" FontSize="20" VerticalAlignment="Center" Grid.Column="1" Foreground="Black"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Background" Value="Green"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
上面的写法等效于下面的:
<Button x:Name="SystemSetting" Cursor="Hand" Click="Button_Click">
<Button.Template>
<ControlTemplate TargetType="Button">
<Grid Background="Transparent">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Image Source="Image/systemsetting.png" Stretch="Uniform" Width="30" VerticalAlignment="Center" Grid.Column="0" Margin="5,0,3,0"/>
<TextBlock Text="系统设置" FontSize="20" FontWeight="Bold" VerticalAlignment="Center" Grid.Column="1"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Background" Value="Green"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Button.Template>
</Button>