关于数据更改的WPF Color Animation

时间:2018-01-26 07:48:16

标签: c# wpf

我试图在属性的数据更改上为标签的背景颜色设置动画。标签正在将颜色更改为值3.但是,如果属性返回值1,则动画将保留在上一个颜色上。我的XAML在下面。

<Style x:Key="Colors" TargetType="Label">
        <Style.Triggers>

            <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=Window},
     Path=DataContext.ColorNo}" Value="1">
                <DataTrigger.EnterActions>
                    <BeginStoryboard>
                        <Storyboard>
                            <ColorAnimation 
                 Storyboard.TargetProperty="(Background).(SolidColorBrush.Color)" 
                 To="Red"/>
                        </Storyboard>
                    </BeginStoryboard>
                </DataTrigger.EnterActions>
            </DataTrigger>

            <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=Window},
     Path=DataContext.ColorNo}" Value="2">
                <DataTrigger.EnterActions>
                    <BeginStoryboard>
                        <Storyboard>
                            <ColorAnimation 
                 Storyboard.TargetProperty="(Background).(SolidColorBrush.Color)" 
                 To="Yellow"/>
                        </Storyboard>
                    </BeginStoryboard>
                </DataTrigger.EnterActions>
            </DataTrigger>

            <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=Window},
     Path=DataContext.ColorNo}" Value="3">
                <DataTrigger.EnterActions>
                    <BeginStoryboard>
                        <Storyboard>
                            <ColorAnimation 
                 Storyboard.TargetProperty="(Background).(SolidColorBrush.Color)" 
                 To="Green"/>
                        </Storyboard>
                    </BeginStoryboard>
                </DataTrigger.EnterActions>
            </DataTrigger>

        </Style.Triggers>
    </Style>

标签XAML

<Label x:Name="vClock"  Content="{Binding Path=Clock,Mode=OneWay}" FontSize="25" Style="{StaticResource Colors}">                    
            </Label>

1 个答案:

答案 0 :(得分:2)

我认为您需要先删除其他活动的故事板,但最好继续在资源中声明您的故事板:

<Storyboard x:key="Value 1">
    <ColorAnimation 
             Storyboard.TargetProperty="(Background).(SolidColorBrush.Color)" 
             To="Red"/>
</Storyboard>

<Storyboard x:key="Value 2">
    <ColorAnimation 
            Storyboard.TargetProperty="(Background).(SolidColorBrush.Color)" 
             To="Yellow"/>
</Storyboard>

<Storyboard x:key="Value 3">
    <ColorAnimation 
             Storyboard.TargetProperty="(Background).(SolidColorBrush.Color)" 
             To="Green"/>
</Storyboard>

然后查看datatrigger

<Style x:Key="Colors" TargetType="Label">
    <Style.Triggers>

        <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=Window},
 Path=DataContext.ColorNo}" Value="1">
            <DataTrigger.EnterActions>
                <StopStoryboard BeginStoryboardName="Value2" />
                <StopStoryboard BeginStoryboardName="Value3" />
                <BeginStoryboard Storyboard="{StaticResource Value1}"
                                     x:Name="Value1" />
            </DataTrigger.EnterActions>
        </DataTrigger>

        ...
</Style>

对所有触发器执行相同操作