我自己重写了按钮,按钮是浅红色。
我想要的效果是,当鼠标进入按钮区域后,按钮慢慢地重浅红色,变成深红色。该怎么实现?有没有例子?关键要解决的是,如何慢慢变成深红色,而不是瞬间变成深红色。

解决方案 »

  1.   

    例如,下面的代码,只能够实现瞬间变化,但不能实现慢慢变化。我希望有一个过程,就像Button类,默认呈现的那种慢慢变化的效果。<Style TargetType="{x:Type Button}">
        <Setter Property="Background" Value="Blue" />
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Background" Value="Yellow" />
            </Trigger>
        </Style.Triggers>
    </Style>
      

  2.   

    感觉自己写,太过于麻烦了吧?我只是想知道,Button类,也有自己默认的样式模板吧?默认的效果,是怎么通过模板实现的?我感觉只要读懂了Button类原来默认的模板,就应该知道答案了。不过我不知道Button类的默认的模板,从哪里可以获得?
      

  3.   

    WPF的话,是用动画做的。建议用blend做。
      

  4.   


    找到了。你们都错了,不用写线程。直接写Xaml样式就可以了。<Window.Resources>        <ResourceDictionary>            <ControlTemplate x:Key="mystyle" TargetType="{x:Type Button}">                <ControlTemplate.Resources>                    <Storyboard x:Key="Timeline1">                        <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="glow" Storyboard.TargetProperty="(UIElement.Opacity)">                            <SplineDoubleKeyFrame KeyTime="00:00:00.3000000" Value="1"/>                        </DoubleAnimationUsingKeyFrames>                    </Storyboard>                    <Storyboard x:Key="Timeline2">                        <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="glow" Storyboard.TargetProperty="(UIElement.Opacity)">                            <SplineDoubleKeyFrame KeyTime="00:00:00.3000000" Value="0"/>                        </DoubleAnimationUsingKeyFrames>                    </Storyboard>                </ControlTemplate.Resources>                <Border BorderBrush="#FFFFFFFF" BorderThickness="1,1,1,1" CornerRadius="4,4,4,4">                    <Border x:Name="border" Background="#00000000" BorderBrush="#FF000000" BorderThickness="1,1,1,1" CornerRadius="4,4,4,4">                        <Grid>                            <Grid.RowDefinitions>                                <RowDefinition Height="0.507*"/>                                <RowDefinition Height="0.493*"/>                            </Grid.RowDefinitions>                            <Border Opacity="0" Background="Red" HorizontalAlignment="Stretch" x:Name="glow" Width="Auto" Grid.RowSpan="2" CornerRadius="4,4,4,4">                            </Border>                            <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" Width="Auto" Grid.RowSpan="2"/>                            <Border HorizontalAlignment="Stretch" Margin="0,0,0,0" x:Name="shine" Width="Auto" CornerRadius="4,4,4,4">                            </Border>                        </Grid>                    </Border>                </Border>                <ControlTemplate.Triggers>                    <Trigger Property="IsMouseOver" Value="True">                        <Trigger.EnterActions>                            <BeginStoryboard Storyboard="{StaticResource Timeline1}"/>                        </Trigger.EnterActions>                        <Trigger.ExitActions>                            <BeginStoryboard x:Name="Timeline2_BeginStoryboard" Storyboard="{StaticResource Timeline2}"/>                        </Trigger.ExitActions>                    </Trigger>                </ControlTemplate.Triggers>            </ControlTemplate>        </ResourceDictionary>    </Window.Resources>        <Grid>        <StackPanel>            <Button Content="Funktionsliste1" VerticalAlignment="Center" Template="{DynamicResource ResourceKey=mystyle}"/>            <Button Content="Funktionsliste2" VerticalAlignment="Center" Template="{DynamicResource ResourceKey=mystyle}"/>        </StackPanel>    </Grid>
      

  5.   

    自定义控件,继承button,重写