c# - 数据更改时的 WPF 彩色动画

标签 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>

最佳答案

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

<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.Triggers>
</Style>

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

关于c# - 数据更改时的 WPF 彩色动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48457452/

相关文章:

c# - 当值是 C# 中的对象列表时,如何查找字典的键?

c# - 拆分时间跨度并在 csv 中创建新字段

c# - 指定的类型转换无效?

c# double 到字符数组或替代

wpf - 如何在 WPF 中编写条件语句?

WPF 文本框焦点

c# - Expression<Action> 和 Expression<Action<T>> 之间的重载优先级

wpf - 在 DataGrid 行鼠标悬停时显示带有绑定(bind)的弹出窗口

.net - 如何在 WCF 中为多个服务配置一个端点?

c# - 计算器总失败