问题动画WPF按钮

时间:2010-09-27 16:26:42

标签: wpf xaml button

我有一个按钮,这是一个独特的按钮,他的风格应该与其他所有人的风格不相符。因此,当您将鼠标移到此按钮上时,它应该更改其图像。但它不起作用,这是代码......我是从WPF开始的,所以如果你能指出我做错了什么就会非常感激

<Button Name="RemoveButton" ClickMode="Press"  BorderThickness="0" Background="Transparent" Style="{StaticResource ButtonStyle1}">
        <Button.Content>
            <Grid>
                <Image x:Name="CloseActive" x:FieldModifier="public" Height="12" VerticalAlignment="Center" Source="/HTFS.Atlas.Portfolio.PortfolioClient.WCF.Controls;component/Images/tab-close.png" Visibility="Hidden" />
                <Image x:Name="CloseInactive" x:FieldModifier="public" Height="12" VerticalAlignment="Center" Source="/HTFS.Atlas.Portfolio.PortfolioClient.WCF.Controls;component/Images/tab-close-inactive.png" />
            </Grid>
        </Button.Content>
        <Button.Resources>
            <Storyboard x:Key="MouseOverAnimation">
                <DoubleAnimation Storyboard.TargetName="CloseActive" Storyboard.TargetProperty="Opacity" From="0" To="1" Duration="0:0:0.5" />
                <DoubleAnimation Storyboard.TargetName="CloseInactive" Storyboard.TargetProperty="Opacity" From="1" To="0" Duration="0:0:0.5" />
            </Storyboard>
            <Storyboard x:Key="MouseOutAnimation">
                <DoubleAnimation Storyboard.TargetName="CloseInactive" Storyboard.TargetProperty="Opacity" From="0" To="1" Duration="0:0:0.5" />
                <DoubleAnimation Storyboard.TargetName="CloseActive" Storyboard.TargetProperty="Opacity" From="1" To="0" Duration="0:0:0.5" />
            </Storyboard>
            <Style x:Key="CloseButtonStyle" TargetType="{x:Type Control}">
                <Style.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Trigger.EnterActions>
                            <BeginStoryboard Storyboard="{StaticResource MouseOverAnimation}" />
                        </Trigger.EnterActions>
                        <Trigger.ExitActions>
                            <BeginStoryboard Storyboard="{StaticResource MouseOutAnimation}" />
                        </Trigger.ExitActions>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </Button.Resources>
    </Button>

1 个答案:

答案 0 :(得分:1)

对于CloseActive图像,请尝试使用Visibility="Hidden" Opacity="0"。 在你的动画中,你正在调整图像的不透明度,但它仍然是隐藏的。

除了删除visibility属性外,请尝试将动画触发器直接放在Button.Triggers

        <Button.Triggers>
            <EventTrigger RoutedEvent="Button.MouseEnter">
                <BeginStoryboard Storyboard="{StaticResource MouseOverAnimation}" />
            </EventTrigger>
            <EventTrigger RoutedEvent="Button.MouseLeave">
                <BeginStoryboard Storyboard="{StaticResource MouseOutAnimation}" />
            </EventTrigger>
        </Button.Triggers>

因为您无法在从样式调用的故事板中使用Storyboard.TargetName。您还必须使用事件触发器而不是普通触发器。 您也可以删除CloseButtonStyle,因为它不会被使用。