2011-03-07 58 views

回答

5

下面是用戶控件的xaml,它將在TextBlock的Text屬性發生更改時爲項目的不透明度設置動畫。

它正在使用PropertyChangedTrigger和ControlStoryboard操作來引起這種情況發生。這些項目來自該得到安裝了混合的dll,但你可以單獨安裝他們,如果你不具備混合: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> 

下面是用於點擊事件,這改變了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); 
} 
+0

謝謝你的代碼。太好了。但是,我不會點擊文本塊。我將動態更新文本。我只需要在動畫發生時運行。在WPF中它會容易得多,但我需要它是Silverlight。有任何想法嗎? – vladc77 2011-03-07 23:05:46

+0

在這個例子中,動畫會在任何時候運行「textBlock.Text」被改變。我只是爲了演示而改變它,但它可以從任何地方改變。 – grimus 2011-03-07 23:21:20

+0

我有最新的SDK。我找不到交互/交互行爲。我應該安裝較舊的SDK嗎?謝謝。 – vladc77 2011-03-08 05:09:48

相關問題