0

这把我难住了。所以我有以下示例应用程序,当鼠标悬停在边框元素上时,它应该为边框元素的不透明度设置动画。`

<UserControl.Resources>
    <Style x:Key="borderstyle" TargetType="ContentControl">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ContentControl">
                    <Grid>

                        <VisualStateManager.VisualStateGroups>

                            <VisualStateGroup>
                                <VisualState x:Name="FirstState">
                                    <Storyboard>
                                        <DoubleAnimation To="1.0" Storyboard.TargetName="border" Storyboard.TargetProperty="Opacity" FillBehavior="HoldEnd"/>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>

                        </VisualStateManager.VisualStateGroups>

                        <Border Background="Blue" x:Name="border" Opacity="0.0"/>

                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>

    </Style>
</UserControl.Resources>


<Grid x:Name="LayoutRoot" Background="White">
    <Border BorderBrush="Red" BorderThickness="1" Width="51" Height="51">
        <ContentControl Width="50" Height="50" Style="{StaticResource borderstyle}"  MouseEnter="OnMouseEntered" />
    </Border>
</Grid>

`

在后面的代码中,我有以下代码......

    private void OnMouseEntered(object sender, MouseEventArgs e)
    {
        bool status = VisualStateManager.GoToState(this, "FirstState", true);
    }

而且……什么也没有发生。status总是 false 并且动画永远不会触发。

我不确定这里缺少什么。

4

3 回答 3

3

您应该指定您ContentControl作为控件正在更改的状态:

private void OnMouseEntered(object sender, MouseEventArgs e)
{
    bool status = VisualStateManager.GoToState((ContentControl)sender, "FirstState", false);
}
于 2010-12-17T17:48:28.803 回答
1

添加ApplyTemplate()到您的控件(在动态(以编程方式)添加控件时需要。

于 2012-03-14T23:32:55.180 回答
1

在 Page Loaded 事件处理程序中或之后随时使用 VisualStateManager 进行状态转换

于 2013-04-03T17:22:44.723 回答