在 WPF 中使用故事板为网格背景颜色设置动画

Animate grid background color using storyboard in WPF

我是 WPF 故事板的新手。我想为网格背景颜色设置动画。我收到此错误:

System.InvalidOperationException: 'Cannot resolve all property references in the property path 'Background.Color'. Verify that applicable objects support the properties.'

我的XAML代码:

<Grid x:Name="alert_grid">
<Grid.Resources>
<Storyboard x:Key="flashing_storyboard" Storyboard.TargetName="alert_grid">
<ColorAnimation 
 Storyboard.TargetProperty="Background.Color"
 From="Black" To="Orange" Duration="0:0:2"
 AutoReverse="True" RepeatBehavior="Forever" />
</Storyboard>
</Grid.Resources>
</Grid>

后面的代码:

Storyboard sb = alert_grid.FindResource("flashing_storyboard") as Storyboard;
if (sb != null)
{
    sb.Begin();
}

知道如何使背景颜色动画化吗?

请试试这个。

<Grid Name="alert_grid" Height="75" HorizontalAlignment="Left" Margin="27,12,0,0" VerticalAlignment="Top" Width="160" Background="LightGray">
             <Grid.Triggers>
                <EventTrigger RoutedEvent="Button.MouseLeave">
                    <BeginStoryboard>
                        <Storyboard x:Name ="flashing_storyboard" Storyboard.Target="{Binding ElementName=alert_grid}">
                        <ColorAnimation To="Orange" Storyboard.TargetProperty="(Grid.Background).(SolidColorBrush.Color)" From="Black" 
         AutoReverse="True" RepeatBehavior="Forever" FillBehavior="Stop" Duration="0:0:2"/>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
           </Grid.Triggers>
    </Grid>

和代码隐藏

Storyboard sb = alert_grid.FindName("flashing_storyboard") as Storyboard;
if (sb != null)
{
   sb.Begin();
}