首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

WPF,控件的可视状态,看下这段代码,该如何处理

2013-08-06 
WPF,控件的可视状态,看下这段代码GridRectangle NameRectangle1 MouseEnterColorChangeMouseEvent

WPF,控件的可视状态,看下这段代码


<Grid>
    <Rectangle Name="Rectangle1" MouseEnter="ColorChangeMouseEvent" Margin="198,17,0,0" Height="88" 
VerticalAlignment="Top" HorizontalAlignment="Left" Width="100">
        <VisualStateManager.VisualStateGroups>
            <VisualStateGroup Name="MouseStates">
                    <VisualState Name="BlueState" />
                <VisualStateGroup.Transitions>
                        <VisualTransition To="BlueState" GeneratedDuration="0:0:2">
                            <VisualTransition.Storyboard>
                                <Storyboard>
                                    <ColorAnimation To="Blue" Storyboard.TargetName="rectBrush" Storyboard.TargetProperty="Color"/>
                                </Storyboard>
                            </VisualTransition.Storyboard>
                        </VisualTransition>                       
                    </VisualStateGroup.Transitions>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
        <Rectangle.Fill>
            <SolidColorBrush x:Name="rectBrush" Color="Orange"/>


        </Rectangle.Fill>
    </Rectangle>
</Grid>




private void ColorChangeMouseEvent(object sender, MouseEventArgs e)
{
    if (Rectangle1.IsMouseOver)
    {
        VisualStateManager.GoToElementState(Rectangle1, "BlueState", true);
    }
}


此例中,只管理了一种状态。设置了状态转换时间为2秒。
两个问题:
1:当鼠标移到矩形上时,为什么颜色立即就开始渐变,直到Blue ?
2:通过第一次鼠标移到矩形上,颜色变了之后,把鼠标移开,等一会儿,再把鼠标移到矩形上,为什么没有反应了,颜色不变了呢,这是为什么?
[解决办法]
我想我明白你的疑问了。
你所想的2秒之后才渐变,指的是VisualState里面如果有动画的话,那么这个动画才开始执行,因为2秒之后才是到达VisaulState的时间点,这时VisaulState里的动画才开始启动。

而你的例子里的动画是放在VisualTransition里的,所以就有了你看到的情况。

热点排行