wpf Storyboard DoubleAnimation ColorAnimation

FredGrit發表於2024-03-29
<Window x:Class="WpfApp24.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp24" WindowState="Maximized"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="100"/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Button Content="Move Circle" Grid.Row="0" FontSize="30">
            <Button.Triggers>
                <EventTrigger RoutedEvent="Button.Click">
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation Storyboard.TargetName="elp"
                                             Storyboard.TargetProperty="(Canvas.Left)"
                                             To="1400" Duration="0:0:5"
                                             AutoReverse="True"/>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </Button.Triggers>
        </Button>
        <Canvas Grid.Row="1">
            <Ellipse x:Name="elp" Fill="Blue" Width="200" Height="200"  Grid.Row="1" Canvas.Left="10"/>
        </Canvas> 
    </Grid>
</Window>

<Window x:Class="WpfApp25.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp25"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800"> 
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="50"/>
            <RowDefinition Height="auto"/>
        </Grid.RowDefinitions>
        <Button Grid.Row="0" Content="Animate Color" Margin="4">
            <Button.Triggers>
                <EventTrigger RoutedEvent="Button.Click">
                    <BeginStoryboard>
                        <Storyboard>
                            <ColorAnimation To="Red" Duration="0:0:5"
                                            Storyboard.TargetName="g1"
                                            Storyboard.TargetProperty="Color"
                                            AutoReverse="True"/>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </Button.Triggers>
        </Button>
        <Canvas Grid.Row="1">
            <Rectangle x:Name="rect" Canvas.Left="20" Width="1400" Height="800">
                <Rectangle.Fill>
                    <LinearGradientBrush EndPoint="1,0">
                        <GradientStop Offset="0" Color="Yellow"/>
                        <GradientStop Offset="1" Color="Black" x:Name="g1"/>
                    </LinearGradientBrush>
                </Rectangle.Fill>
            </Rectangle>
        </Canvas> 
    </Grid>
</Window>

相關文章