WPF中Button的ControlTemplate的问题
现在有一个Button,为了在贴图后去掉默认的边框用了ControlTemplate,同时实现一个当鼠标划过时换图片的效果
<Window.Resources> <Style x:Key="ss" TargetType="Image"> <Setter Property="Source" Value="/image/nav-button.png"/> <Style.Triggers> <Trigger Property="Image.IsMouseOver" Value="true"> <Setter Property="Source" Value="/image/nav-button_hover.png"/> </Trigger> </Style.Triggers> </Style> </Window.Resources> <Button Content="用户查找" Height="23" HorizontalAlignment="Left" Margin="-161,32,0,0" Name="bt_Find" VerticalAlignment="Top" Width="95" Click="bt_Find_Click"> <Button.Template> <ControlTemplate> <Grid> <Image Name="Image_Find" Style="{StaticResource ss}" /> <TextBlock Text=" 用户查找" FontSize="12" HorizontalAlignment="Center" VerticalAlignment="Center"/> </Grid> </ControlTemplate> </Button.Template> </Button>
<!--按钮背景画刷-->
<LinearGradientBrush x:Key="buttonBackgroundBrush">
<GradientStop Offset="0" Color="Beige"/>
<GradientStop Offset="1" Color="White"/>
</LinearGradientBrush>
<!--按钮模板定义-->
<ControlTemplate x:Key="TestButtonTemplate" TargetType="{x:Type Button}">
<Border x:Name="border" BorderThickness="1" BorderBrush="Black"
Background="{StaticResource buttonBackgroundBrush}"
CornerRadius="10">
<Grid>
<Image x:Name="image" Source="a.png"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Stretch="Fill" Height="50" Width="50"/>
<Label Content="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=Content}"
HorizontalAlignment="Center"
VerticalAlignment="Center"/>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter TargetName="image" Property="Source" Value="b.png"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
<!--按钮样式定义,实际只需要在Button的Style中引用这个就好-->
<Style x:Key="TestButtonStyle" TargetType="{x:Type Button}">
<Setter Property="FontSize" Value="15"/>
<Setter Property="FontWeight" Value="bold"/>
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="Foreground" Value="Blue"/>
<Setter Property="Template" Value="{StaticResource TestButtonTemplate}"/>
</Style>