Silverlight动画样式来自资源文件

时间:2012-09-06 18:57:52

标签: c# silverlight silverlight-4.0

我想在鼠标输入边框时为按钮阴影效果颜色设置动画。 我试试这个代码并不适合我。 我不知道问题出在哪里?

    <Style x:Name="HeaderButton" TargetType="Button">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Border x:Name="Border" BorderBrush="#FF550211" BorderThickness="0" CornerRadius="4">
                    <Border.Triggers>
                        <EventTrigger RoutedEvent="Border.MouseEnter">
                            <BeginStoryboard>
                                <Storyboard>
                                    <ColorAnimation Storyboard.TargetProperty="Color" Storyboard.TargetName="MenuButtonShadow"
                                                    From="#FFFFFFFF" To="#FF000000" Duration="0:0:0.3"></ColorAnimation>
                                </Storyboard>
                            </BeginStoryboard>
                        </EventTrigger>
                    </Border.Triggers>
                    <Border.Background>
                        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                            <GradientStop Color="#FFAF1232" Offset="0" />
                            <GradientStop Color="#FFB60329" Offset="1" />
                        </LinearGradientBrush>
                    </Border.Background>
                    <ContentPresenter Margin="8, 0" VerticalAlignment="Center" HorizontalAlignment="Center" />
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="Effect">
        <Setter.Value>
            <DropShadowEffect x:Name="MenuButtonShadow" ShadowDepth="0" BlurRadius="4"></DropShadowEffect>
        </Setter.Value>
    </Setter>
    <Setter Property="Foreground" Value="White"></Setter>
    <Setter Property="Margin" Value="4"></Setter>
</Style>

1 个答案:

答案 0 :(得分:1)

来自MSDN

  

在Silverlight中,您可以用于EventTrigger的唯一事件是Loaded事件。对于其他事件,您应该在Resources属性中声明一个storyboard,为storyboard提供一个Name值,并编写一个在指定的storyboard上调用Begin方法的事件处理程序。

我想你可以在你的XAML中做这样的事情(虽然我还没有测试过):

<Storyboard x:Name="MenuButtonShadowStoryboard">
    <ColorAnimation Storyboard.TargetProperty="Color" Storyboard.TargetName="MenuButtonShadow"
                    From="#FFFFFFFF" To="#FF000000" Duration="0:0:0.3">
    </ColorAnimation>
</Storyboard>

<ControlTemplate TargetType="Button">
    <Border x:Name="Border" MouseEnter="Border_MouseEnter">
        <!-- omitted for brevity -->
    </Border>
</ControlTemplate>

然后您的事件处理程序将在故事板上调用Begin方法:

private void Border_MouseEnter(object sender, MouseEventArgs e)
{
    MenuButtonShadowStoryboard.Begin();
}

您可能希望为MouseLeave事件添加类似的内容,这会使颜色返回到#FFFFFFFF。