读书人

WPF请看一下控件可视状态的代码解决

发布时间: 2013-08-06 16:47:25 作者: rapoo

WPF,请看一下控件可视状态的代码


<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);
}
}  



问题:
当鼠标移到按钮上时,是突然变成绿色的,而移开按钮时,却是渐变成桔色的。都是线性插值动画,为什么鼠标移到按钮上不是渐变成绿色的呢?
[解决办法]
看看原色是什么,估计是到绿色的渐变不明显。
[解决办法]
            <Button.Background>
<SolidColorBrush x:Name="rectBrush" Color="Orange"/>
</Button.Background>

看到了,你把两个颜色换过来试试
[解决办法]
button有自己的鼠标渐变效果,要禁用掉它原来的渐变效果,你的效果才看得出。
在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>

读书人网 >C#

热点推荐