找到你要的答案

Q:Fading animation in Visual Studio Blend

Q:VisualStudio混合中的衰落动画

I wish to change the actual content of my textblock. I know I can change it by using textblock.text = "new text" when an event is triggered. However, is there a way to make it change gradually, i.e. with a fade animation? I tried using Visual Studio Blend, and managed to make objects scale, and rotate, but I have not figured out how to make content of an element change/ apply animation when content of the element changes.

我想改变我的TextBlock的实际内容。我知道我可以改变它,用textblock.text =“新文本”的触发事件发生时。然而,有没有办法使它逐渐改变,即与褪色动画?我尝试使用Visual Studio混合,并设法使对象缩放,并旋转,但我还没有弄清楚如何使元素的内容改变/应用动画时,元素的内容变化。

answer1: 回答1:

Here is a small example, which consists of a TextBox and a TextBlock. Each time you change the value in the TextBox, the content of the TextBlock fades out and in.

<StackPanel>
        <TextBox x:Name="txtSampleText" Margin="10" Text="This is a Sample Text"></TextBox>
        <TextBlock x:Name="tbMessage" Text="{Binding ElementName=txtSampleText, Path=Text, NotifyOnTargetUpdated=True}" Margin="10">
            <TextBlock.Triggers>
                <EventTrigger RoutedEvent="Binding.TargetUpdated">
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation Storyboard.TargetProperty="Opacity" Duration="0:0:1" From="1.0" To="0.0" />
                            <DoubleAnimation Storyboard.TargetProperty="Opacity" Duration="0:0:1" From="0.0" To="1.0"  />
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </TextBlock.Triggers>
        </TextBlock>
 </StackPanel>

这里是一个小例子,它由一个文本框和一个TextBlock。每一次你改变文本框中的值,内容的TextBlock的淡出。

<StackPanel>
        <TextBox x:Name="txtSampleText" Margin="10" Text="This is a Sample Text"></TextBox>
        <TextBlock x:Name="tbMessage" Text="{Binding ElementName=txtSampleText, Path=Text, NotifyOnTargetUpdated=True}" Margin="10">
            <TextBlock.Triggers>
                <EventTrigger RoutedEvent="Binding.TargetUpdated">
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation Storyboard.TargetProperty="Opacity" Duration="0:0:1" From="1.0" To="0.0" />
                            <DoubleAnimation Storyboard.TargetProperty="Opacity" Duration="0:0:1" From="0.0" To="1.0"  />
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </TextBlock.Triggers>
        </TextBlock>
 </StackPanel>
c#  xaml  visual-studio-2013  windows-8.1