C# 基于启动时属性更改触发器的WPF动画

C# 基于启动时属性更改触发器的WPF动画,c#,wpf,wpf-animation,C#,Wpf,Wpf Animation,当绑定到textbox的文本属性的值更改时,我尝试设置边框颜色的动画: <Border x:Name="border" BorderThickness="3" BorderBrush="Blue" HorizontalAlignment="Center" VerticalAlignment="Center"> <TextBlock Background="White" TextAlignment="Center" FontSize="24"

当绑定到textbox的文本属性的值更改时,我尝试设置边框颜色的动画:

<Border x:Name="border" BorderThickness="3" BorderBrush="Blue" HorizontalAlignment="Center" VerticalAlignment="Center">
    <TextBlock Background="White" TextAlignment="Center" FontSize="24" 
                       Text="{Binding Value, NotifyOnTargetUpdated=True}">
        <TextBlock.Triggers>
            <EventTrigger RoutedEvent="Binding.TargetUpdated">
                <EventTrigger.Actions>
                    <BeginStoryboard>
                        <Storyboard>
                            <ColorAnimation To="Yellow"
                                Storyboard.TargetName="border"
                                AutoReverse="True"
                                Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)"
                                FillBehavior="Stop"
                                Duration="0:0:0.5"
                                RepeatBehavior="5x" />
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger.Actions>
            </EventTrigger>
        </TextBlock.Triggers>
    </TextBlock>
</Border>
问题是,即使在应用程序启动且尚未设置一次值时,动画也会被触发。
有没有解决方法可以避免这种情况?

下面是我建议的解决方法的演示。这使用了Nuget软件包免责声明:我是ViewModel基类和ICommand实现的这个软件包的作者,但是您应该能够轻松地替换您使用的任何系统

如果我为您找到更好的选择,我将添加它作为附加答案

MainWindow.xaml注意:TextBlock上的EventTrigger已更改为边框上的DataTrigger:


所有这些都绑定到视图模型了吗?一旦加载了TextBlock,或者在第一次引发Binding.TargetUpdated之后,您可以通过编程方式添加触发器。@BradleyUffner是的,这是一个实现INotifyPropertyChangedYou的简单视图模型。您可以从您控制的其他对象触发动画。我知道这并不理想,但您可以使用bool属性来确定动画是否应该播放,触发该属性上的动画为true,然后在值设置器中将该属性设置为true。我正在寻找更好的选择,但WPF动画是一个我还没有太多经验的领域。
<Window
    x:Class="BindingHelp.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="clr-namespace:BindingHelp"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    Title="MainWindow"
    Width="525"
    Height="350"
    mc:Ignorable="d">
    <Window.DataContext>
        <local:MainWindowVm />
    </Window.DataContext>
    <StackPanel Orientation="Vertical">
        <Border
            x:Name="border"
            HorizontalAlignment="Center"
            VerticalAlignment="Center"
            BorderBrush="Blue"
            BorderThickness="3">
            <Border.Style>
                <Style>
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Path=ShouldAnimate}" Value="true">
                            <DataTrigger.EnterActions>
                                <BeginStoryboard>
                                    <Storyboard>
                                        <ColorAnimation
                                            AutoReverse="True"
                                            FillBehavior="Stop"
                                            RepeatBehavior="5x"
                                            Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)"
                                            To="Yellow"
                                            Duration="0:0:0.5" />
                                    </Storyboard>
                                </BeginStoryboard>
                            </DataTrigger.EnterActions>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </Border.Style>
            <TextBlock
                MinWidth="200"
                Background="White"
                FontSize="24"
                Text="{Binding Value, NotifyOnTargetUpdated=True}"
                TextAlignment="Center" />
        </Border>
        <Button Command="{Binding Path=SetValueCommand}" Content="Update Value" />
    </StackPanel>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using AgentOctal.WpfLib;
using AgentOctal.WpfLib.Commands;

namespace BindingHelp
{
    class MainWindowVm : ViewModel
    {
        private string _value;
        public string Value
        {
            get { return _value; }
            set
            {
                if (SetValue(ref _value, value))
                {
                    ShouldAnimate = true;
                }
            }
        }

        private bool _shouldAnimate = false;
        public bool ShouldAnimate
        {
            get { return _shouldAnimate; }
            set { SetValue(ref _shouldAnimate, value); }
        }


        private ICommand _setValueCommand;
        public ICommand SetValueCommand
        {
            get
            {
                return _setValueCommand ?? (_setValueCommand = new SimpleCommand((obj) =>
                {
                    Value = "This is a test!";
                }));
            }
        }    
    }
}