【问题标题】:Animate background colour on exitactionexitaction 上的动画背景颜色
【发布时间】:2017-02-28 23:05:12
【问题描述】:

我正在尝试为网格上的背景颜色设置动画以更改,一旦事件发生,但我无法让它工作,我可以让它立即改变颜色(通过数据触发器),但只要我尝试在其中引入动画,然后我无法让它工作(动画似乎没有生效)。

这是我正在使用的当前 XAML(尽管我尝试了各种变体,但无法让它动画化):

<DataTrigger Binding="{Binding ElementName=me, Path=Viewed}" Value="False">
    <Setter Property="Background" Value="LightYellow" />
    <DataTrigger.ExitActions>
        <BeginStoryboard>
            <Storyboard>
                <ColorAnimation Duration="00:00:02" To="White" Storyboard.TargetProperty="(Grid.Background).(SolidColorBrush.Color)"/>
            </Storyboard>
        </BeginStoryboard>
    </DataTrigger.ExitActions>
</DataTrigger>
<!--
<DataTrigger Binding="{Binding ElementName=me, Path=Viewed}" Value="True">
    <Setter Property="Background" Value="White" />
</DataTrigger>
-->

查看的位置是我的控件上的依赖属性(布尔值)。任何正确方向的提示将不胜感激。我还尝试将其设置为引发事件的 EventTrigger,该事件在 bool 切换为 true 时发生。

【问题讨论】:

  • Grid 的Background 是否真的设置为modifiable SolidColorBrush?不像Background="Black"
  • 这也是我尝试过的方法之一,拥有一个静态资源 SolidColorBrush,并对其进行动画处理,动画通过事件触发器工作,但我无法将原始属性设置为 LightYellow , 仅当值为 false 时
  • 我的意思是,如果你想为(Grid.Background).(SolidColorBrush.Color) 设置动画,Grid.Background 必须设置为 SolidColorBrush 的应用程序定义实例。

标签: wpf xaml


【解决方案1】:

感谢 Clemens 的帮助,弄清楚我需要做什么:

    <SolidColorBrush x:Key="GridColourBrush" Color="LightYellow" />
        <Style x:Key="GridStyle" TargetType="Grid">
            <Setter Property="Background" Value="White" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=me, Path=Viewed}" Value="False">
                    <Setter Property="Background" Value="{StaticResource GridColourBrush}" />
                    <DataTrigger.ExitActions>
                        <BeginStoryboard>
                            <Storyboard>
                                <ColorAnimation Duration="00:00:02" To="White" Storyboard.TargetProperty="(Grid.Background).(SolidColorBrush.Color)"/>
                            </Storyboard>
                        </BeginStoryboard>
                    </DataTrigger.ExitActions>
                </DataTrigger>
            </Style.Triggers>
        </Style>
        <!-- snipped stuff -->
        <Grid MinWidth="525" x:Name="ContainerGrid" Style="{StaticResource GridStyle}" Background="{StaticResource GridColourBrush}" />

所以默认将背景设置为纯白色,然后如果 DP bool 为 false,则将背景更改为静态纯色画笔,然后我可以通过退出操作对其进行动画处理。

【讨论】:

    【解决方案2】:

    我的意思很简单,而不是

    <Grid Background="LightYellow">
    </Grid>
    

    你必须写

    <Grid>
        <Grid.Background>
            <SolidColorBrush Color="LightYellow" />
        </Grid.Background>
    </Grid>
    

    不需要额外的资源。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-13
      • 2012-12-18
      • 2023-03-11
      • 2010-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-23
      相关资源
      最近更新 更多