1

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

这是我正在使用的当前 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>
-->

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

4

2 回答 2

1

感谢克莱门斯的帮助,弄清楚了我需要做什么:

    <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,则将背景更改为静态纯色画笔,然后我可以通过退出操作对其进行动画处理。

于 2012-08-07T11:27:42.713 回答
0

我的意思很简单,而不是

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

你必须写

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

不需要额外的资源。

于 2012-08-07T11:33:15.507 回答