<Grid>
<Button Name="button2" MouseEnter="ColorChangeMouseEvent" MouseLeave="ColorChangeMouseEvent" Margin="198,17,0,0" Height="88" VerticalAlignment="Top" HorizontalAlignment="Left" Width="100">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup Name="MouseStates">
<VisualState Name="BlueState">
<Storyboard>
<ColorAnimation To="Blue" Storyboard.TargetName="rectBrush" Storyboard.TargetProperty="Color"/>
</Storyboard>
</VisualState>
<VisualState Name="OrangeState">
<Storyboard>
<ColorAnimation To="Orange" Storyboard.TargetName="rectBrush" Storyboard.TargetProperty="Color"/>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Button.Background>
<SolidColorBrush x:Name="rectBrush" Color="Orange"/>
</Button.Background>
</Button>
</Grid> private void ColorChangeMouseEvent(object sender, MouseEventArgs e)
{
if (button2.IsMouseOver)
{
VisualStateManager.GoToElementState(button2, "BlueState", true);
}
else
{
VisualStateManager.GoToElementState(button2, "OrangeState", true);
}
} 问题:
当鼠标移到按钮上时,是突然变成绿色的,而移开按钮时,却是渐变成桔色的。都是线性插值动画,为什么鼠标移到按钮上不是渐变成绿色的呢?
<SolidColorBrush x:Name="rectBrush" Color="Orange"/>
</Button.Background>
看到了,你把两个颜色换过来试试
在button里加一段template模板: <Button Name="button2" MouseEnter="ColorChangeMouseEvent" MouseLeave="ColorChangeMouseEvent" Margin="198,17,0,0" Height="88" VerticalAlignment="Top" HorizontalAlignment="Left" Width="100">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup Name="MouseStates">
<VisualState Name="BlueState">
<Storyboard>
<ColorAnimation To="Blue" Storyboard.TargetName="rectBrush" Storyboard.TargetProperty="Color"/>
</Storyboard>
</VisualState>
<VisualState Name="OrangeState">
<Storyboard>
<ColorAnimation To="Orange" Storyboard.TargetName="rectBrush" Storyboard.TargetProperty="Color"/>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Button.Background>
<SolidColorBrush x:Name="rectBrush" Color="Orange"/>
</Button.Background>
<Button.Template>
<ControlTemplate>
<Border Background="{TemplateBinding Background}">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
</Border>
</ControlTemplate>
</Button.Template>
</Button>