标签 标签 标签
- 一句话的事儿:鼠标放到按钮上颜色是蓝色的,离开后是灰色的
效果介绍
- 鼠标放到按钮上由蓝色变为灰色;
-
代码逻辑介绍
鼠标悬停与离开
- 通过事件触发器来实现
- 变色,由蓝色变为灰色,由灰色变为蓝色
- 变色行为动画通过动画来实现Animation来实现
- 变色是通过Brush的方式来实现
源代码
<Button Width="150" Height="50" Click="Button_Click">
<Button.Template>
<ControlTemplate>
<Grid>
<TextBlock Text="按钮" TextAlignment="Center" VerticalAlignment="Center" FontSize="25" Foreground="Blue"></TextBlock>
<!--<Rectangle x:Name="rec" Width="100" Height="50" Fill="Violet" RadiusX="25" RadiusY="25" Opacity="0.5"></Rectangle>-->
<Rectangle x:Name="rec" Width="100" Height="50" RadiusX="25" RadiusY="25" Opacity="0.5">
<!--将填充部分单独写,为了后面单独调用做准备-->
<Rectangle.Fill>
<!--采用刷子来填充-->
<SolidColorBrush x:Name="recBrush" Color="Blue"></SolidColorBrush>
</Rectangle.Fill>
<Rectangle.Triggers>
<EventTrigger RoutedEvent="MouseEnter">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation Storyboard.TargetName="recBrush" Storyboard.TargetProperty="Color" To="Gray" Duration="0:0:1"></ColorAnimation>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
<EventTrigger RoutedEvent="MouseLeave">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation Storyboard.TargetName="recBrush" Storyboard.TargetProperty="Color" To="Blue" Duration="0:0:1"></ColorAnimation>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Rectangle.Triggers>
</Rectangle>
</Grid>
</ControlTemplate>
</Button.Template>
</Button>
- 本文作者:GeekPower - Felix Sun
- 版权声明: 本博客所有文章除特别声明外,均采用 MIT 许可协议。转载请注明出处!