1

这是我目前用来为可重复使用的简单后退按钮设置动画的代码,问题是我还需要另一个具有相同属性的按钮,但可以根据将要显示的屏幕指定文本用过的。

为了显示效果和文本,我必须添加什么?(我所有添加文本的尝试只是使按钮完全消失。)

<UserControl x:Class="SantaBarbara.Resources.BackButton"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             >
    <Button Width="116" Height="119" Canvas.Top="57" BorderThickness="0" BorderBrush="Transparent">
        <Button.Style>            
            <Style TargetType="{x:Type Button}">
                <Style.Resources>
                    <SolidColorBrush x:Key="BackgroundBrush" Color="#990050" Opacity="0.50" />                    
                </Style.Resources>
                <Setter Property="OverridesDefaultStyle" Value="True"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="Button">

                            <ControlTemplate.Resources>
                                <Storyboard x:Key="MouseOverAnimation">
                                    <DoubleAnimation Duration="0:0:0.5" To="0.9" Storyboard.TargetProperty="(Button.Background).(SolidColorBrush.Opacity)" Storyboard.TargetName="Border" />
                                </Storyboard>
                                <Storyboard x:Key="MouseOutAnimation">
                                    <DoubleAnimation Duration="0:0:0.5" To="0.5" Storyboard.TargetProperty="(Button.Background).(SolidColorBrush.Opacity)" Storyboard.TargetName="Border" />
                                </Storyboard>
                            </ControlTemplate.Resources>

                            <Grid>
                                <Border x:Name="Border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" >
                                    <ContentPresenter x:Name="ContentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}"  Content="{TemplateBinding Content}" HorizontalAlignment="Center" Margin="{TemplateBinding Padding}"/>
                                </Border>
                            </Grid>

                            <ControlTemplate.Triggers>
                                <Trigger Property="IsMouseOver" Value="True">
                                    <Trigger.EnterActions>
                                        <BeginStoryboard Storyboard="{StaticResource MouseOverAnimation}" />
                                    </Trigger.EnterActions>
                                    <Trigger.ExitActions>
                                        <BeginStoryboard Storyboard="{StaticResource MouseOutAnimation}" />
                                    </Trigger.ExitActions>
                                </Trigger>
                            </ControlTemplate.Triggers>

                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Button.Style>        
    </Button>
</UserControl>
4

1 回答 1

1

你需要一点补充。在. Button_ Background_Style

你有你Brush的资源,需要在默认样式设置器中设置它。

就像是:

<Style TargetType="{x:Type Button}">
  <Style.Resources>
    <SolidColorBrush x:Key="BackgroundBrush"
                     Opacity="0.50"
                     Color="#990050" />
  </Style.Resources>
  <Setter Property="OverridesDefaultStyle"
          Value="True" />
  <Setter Property="Background"
          Value="{StaticResource BackgroundBrush}" />
  ...

然后,您可以提取Style,指定 ax:Key并将其应用于您想要的按钮。刚试了一下,动画效果很好

于 2013-03-26T18:51:29.367 回答