如何在文本块字符串上运行故事板在Silverlight中更改?

时间:2011-03-07 02:48:30

标签: silverlight silverlight-4.0

我想知道如果更改了textblock文本字符串,是否有任何简单的技术来运行storyboard。提前谢谢!

1 个答案:

答案 0 :(得分:5)

下面是用户控件的xaml,它将在TextBlock的Text属性发生更改时为项目的不透明度设置动画。

正在使用PropertyChangedTrigger和ControlStoryboard操作来实现此目的。这些项目来自使用Blend安装的dll,但如果您没有Blend,则可以单独安装它们:Blend 4 SDK

<UserControl
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
    xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
    x:Class="TextboxAnimation.MainPage"
    Width="640" Height="480">
    <UserControl.Resources>
        <Storyboard x:Name="AnAnimation">
            <DoubleAnimationUsingKeyFrames
                Storyboard.TargetProperty="(UIElement.Opacity)"
                Storyboard.TargetName="animationTextBlock">
                <EasingDoubleKeyFrame KeyTime="0" Value="0"/>
                <EasingDoubleKeyFrame KeyTime="0:0:1" Value="1"/>
            </DoubleAnimationUsingKeyFrames>
        </Storyboard>
    </UserControl.Resources>

    <StackPanel x:Name="LayoutRoot" Background="White">
        <TextBlock x:Name="textBlock"
            HorizontalAlignment="Left" Text="Click Me To Change Text"
            MouseLeftButtonDown="TextBlockClicked">
            <i:Interaction.Triggers>
                <ei:PropertyChangedTrigger Binding="{Binding Text, ElementName=textBlock}">
                    <ei:ControlStoryboardAction Storyboard="{StaticResource AnAnimation}"/>
                </ei:PropertyChangedTrigger>
            </i:Interaction.Triggers>
        </TextBlock>
        <TextBlock
            x:Name="animationTextBlock"
            Text="Animate Me!" Margin="0,8,0,0" Opacity="0"/>
    </StackPanel>
</UserControl>

以下是用于click事件的代码,它改变了TextBlock Text属性:

int times = 0;

private void TextBlockClicked(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
    times++;

    textBlock.Text = String.Format("I've been clicked and changed {0} times!", times);
}