【问题标题】:Animate grid background color using storyboard in WPF在 WPF 中使用情节提要为网格背景颜色设置动画
【发布时间】:2021-01-26 03:41:40
【问题描述】:

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

System.InvalidOperationException: '无法解析所有属性 属性路径“Background.Color”中的引用。验证 适用对象支持这些属性。'

我的 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();
}

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

【问题讨论】:

  • 背景是画笔而不是颜色。看到无论如何你都有代码,你可以将solidcolorbrush从控件的背景prooerty中取出并在代码中做一个彩色动画。没有任何故事板。

标签: c# .net wpf xaml storyboard


【解决方案1】:

请试试这个。

<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();
}

【讨论】:

    猜你喜欢
    • 2017-01-05
    • 2016-01-02
    • 2012-12-18
    • 2012-10-06
    • 2011-09-14
    • 2015-02-22
    • 1970-01-01
    • 2013-11-23
    • 1970-01-01
    相关资源
    最近更新 更多