WPF文本淡出,然后生效

WPF文本淡出,然后生效,wpf,text,animation,Wpf,Text,Animation,我试图使用WPF动画来创建一种效果,当文本属性中的数据发生变化时,文本淡出,然后再次淡入。。或者最好是适当的交叉淡入度 我已经成功地完成了这项工作的一半,下面的代码响应文本更改事件,立即使文本不可见,然后在3秒钟内淡出 淡出文本同样简单,我只需更改标记的From和To属性。但是,问题是屏幕上的文本会立即改变。当然,这通常是绝对必要的,但在这种情况下,我希望旧文本淡出,然后新文本淡入 在WPF动画中有什么聪明的技巧可以做到这一点吗 当前半成品触发器: <Style TargetType="T

我试图使用WPF动画来创建一种效果,当文本属性中的数据发生变化时,文本淡出,然后再次淡入。。或者最好是适当的交叉淡入度

我已经成功地完成了这项工作的一半,下面的代码响应文本更改事件,立即使文本不可见,然后在3秒钟内淡出

淡出文本同样简单,我只需更改标记的From和To属性。但是,问题是屏幕上的文本会立即改变。当然,这通常是绝对必要的,但在这种情况下,我希望旧文本淡出,然后新文本淡入

在WPF动画中有什么聪明的技巧可以做到这一点吗

当前半成品触发器:

<Style TargetType="TextBlock" x:Key="fadeinout">
        <Style.Triggers>
            <EventTrigger RoutedEvent="Binding.TargetUpdated">
                <BeginStoryboard>
                    <Storyboard>
                        <DoubleAnimation Storyboard.TargetProperty="Opacity" Duration="0:0:3" From="0.0" To="1.0" BeginTime="0:0:0" />
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
        </Style.Triggers>
    </Style>

我认为这不可能在仅XAML的解决方案中实现。TextBlock不知道“旧”和“新”文本,只知道文本

这样做的方法是创建一个从TextBlock派生的自定义控件,并对TargetUpdate事件执行以下操作:

  • 调用“淡出”故事板
  • 更改文本
  • 称为“淡入”故事板

祝您好运:)

此任务的最佳解决方案是使用“过渡演示者”。Transition presenter是控件的容器(可以是TextBlock或其他任何控件),它通过应用指定的转换对内容的更改作出反应。您可以选择一个预定义的转换或创建自己的转换(使用XAML)。通常,transition presenter使用数据模板来显示绑定的数据。最基本的示例如下所示:

<lib:TransitionPresenter Transition="{StaticResource FadeTransition}
    Content="{Binding MyValue}">
    <lib:TransitionPresenter.Resources>
        <DataTemplate DataType="{x:Type System:string}">
            <TextBlock Text={Binding}/>
        </DataTemplate>
    </lib:TransitionPresenter.Resources>
</lib:TransitionPresenter>

关于这件事,我和弗朗西斯是一致的。我要做的是创建一个新的UserControl,它可以通过淡入淡出无缝地处理新旧文本

我正在用一个标签做类似的事情,我只想显示几秒钟,然后消失。以下是我所做的:

<Label  Name="lbl" DockPanel.Dock="Bottom" HorizontalAlignment="Center" Visibility="Collapsed">
    <Label.Style>
        <Style TargetType="{x:Type Label}">
            <Style.Triggers>
                <Trigger Property="Visibility" Value="Visible">
                    <Trigger.EnterActions>
                        <BeginStoryboard>
                            <Storyboard>
                                <DoubleAnimation Storyboard.TargetProperty="Opacity" Duration="00:00:00" BeginTime="00:00:00" From="0.0" To="1.0" />
                                <DoubleAnimation Storyboard.TargetProperty="Opacity" Duration="00:00:03" BeginTime="00:00:02" From="1.0" To="0.0" />
                            </Storyboard>
                        </BeginStoryboard>
                    </Trigger.EnterActions>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Label.Style>
    Display Text
</Label>
您可以轻松地将此示例修改为UserControl。只需更改可见性==隐藏上的淡出,添加一个与可见性==可见相反的情节提要, 并更改文本并重置记号处理程序内的可见性


希望这有帮助

这是一个自动执行淡出、切换值和淡入的实现

要使用(将xmlns:l设置为正确的命名空间后):

Label l:AnimatedSwitch.Property="Content" l:AnimatedSwitch.Binding="{Binding SomeProp}"/>
代码(这是概念验证代码,没有错误处理,但尚未准备好生产)


当然这是可能的,只需使用两个文本块
Label l:AnimatedSwitch.Property="Content" l:AnimatedSwitch.Binding="{Binding SomeProp}"/>
public class AnimatedSwitch : DependencyObject
{
    // Define the attached properties

    public static DependencyProperty BindingProperty =
        DependencyProperty.RegisterAttached("Binding", typeof(object), typeof(AnimatedSwitch),
        new PropertyMetadata(BindingChanged));
    public static DependencyProperty PropertyProperty =
        DependencyProperty.RegisterAttached("Property", typeof(string), typeof(AnimatedSwitch));
    public static object GetBinding(DependencyObject e)
    {
        return e.GetValue(BindingProperty);
    }
    public static void SetBinding(DependencyObject e, object value)
    {
        e.SetValue(BindingProperty, value);
    }
    public static string GetProperty(DependencyObject e)
    {
        return (string)e.GetValue(PropertyProperty);
    }
    public static void SetProperty(DependencyObject e, string value)
    {
        e.SetValue(PropertyProperty, value);
    }

    // When the value changes do the fadeout-switch-fadein

    private static void BindingChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        Storyboard fadeout = new Storyboard();
        var fadeoutAnim = new DoubleAnimation(){To=0,Duration=new Duration(TimeSpan.FromSeconds(0.3))};
        Storyboard.SetTarget(fadeoutAnim,d);
        Storyboard.SetTargetProperty(fadeoutAnim, new PropertyPath("Opacity"));
        fadeout.Children.Add(fadeoutAnim);
        fadeout.Completed += (d1, d2) =>
            {
                d.GetType().GetProperty(GetProperty(d)).SetValue(d, GetBinding(d), null);

                Storyboard fadein = new Storyboard();
                var fadeinAnim = new DoubleAnimation() { To = 1, Duration = new Duration(TimeSpan.FromSeconds(0.3)) };
                Storyboard.SetTarget(fadeinAnim, d);
                Storyboard.SetTargetProperty(fadeinAnim, new PropertyPath("Opacity"));
                fadein.Children.Add(fadeinAnim);
                fadein.Begin();
            };
        fadeout.Begin();
    }
}