【问题标题】:How can I animate a TextBlock when a SelectionChanged() event of a ListBox is called?调用 ListBox 的 SelectionChanged() 事件时,如何为 TextBlock 设置动画?
【发布时间】:2012-07-07 21:44:29
【问题描述】:

在下面的代码中,我想在调用 TextBlock 的 TextChanged() 事件时开始动画。但是当我尝试这段代码时,我得到了一个错误......

“未能分配给属性'System.Windows.EventTrigger.RoutedEvent'”

我迷路了,有人可以帮助我,我该怎么做?

<StackPanel>
   <ListBox Name"lstSample" SelectionChanged="lstSample_SelectionChanged">
       <ListBox.Triggers>
          <EventTrigger RoutedEvent="ListBox.SelectionChanged">
              <BeginStoryboard>
                  <BeginStoryboard.Storyboard>
                      <Storyboard>
                         <DoubleAnimation Storyboard.TargetName="txtSample" Storyboard.TargetProperty="Opacity" From="0" To="1" Duration="0:0:1.0">
                              <DoubleAnimation.EasingFunction>
                                  <PowerEase EasingMode="EaseIn" Power="8"/>
                              </DoubleAnimation.EasingFunction>
                          </DoubleAnimation>
                      </Storyboard>
                  </BeginStoryboard.Storyboard>
              </BeginStoryboard>
          </EventTrigger>
       </ListBoxTriggers>
   </ListBox>

   <Border Name="brdrTextSampleLanguageOne" BorderThickness="0" BorderBrush="{StaticResource PhoneAccentBrush}">
      <TextBlock 
             Text="This is sample text." 
             Name="txtSample" 
             TextAlignment="Right" 
             VerticalAlignment="Center" />

    </Border>
</StackPanel>

非常感谢。

【问题讨论】:

    标签: c# silverlight windows-phone-7 xaml silverlight-4.0


    【解决方案1】:

    使用代码真的很容易,只需创建如下属性:

     private string _textBlockText;
            public string textBlockText
            {
                get { return _textBlockText; }
                set
                {
                    if (txtSample.Text != value)
                    {
                        if (Storyboard1.GetCurrentState() != ClockState.Active)
                            Storyboard1.Begin();
                        txtSample.Text = value;
                    }
                }
            }
    

    只需使用 textBlockText 属性来更新代码中任何位置的文本,这应该像 TextChanged 事件一样工作...注意:Storyboard1 是您希望在 TextChanged 事件上播放的动画。

    【讨论】:

    • 您好,感谢您的回答。我需要将 Storyboard1 放在 XAML 中的什么位置?你有这方面的例子吗?
    • 例如: 。 .你的故事板。 x:Name="CanvasHide">
    【解决方案2】:

    这将帮助您找到下面的代码

    <UserControl x:Class="WrapPanel.MainPage"
                 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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                 mc:Ignorable="d"
                 d:DesignHeight="300"
                 d:DesignWidth="400"
                 xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
                 xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
                 xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit"
                 xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">
    
        <Grid x:Name="LayoutRoot"
              Background="White">
            <StackPanel>
                <StackPanel.Resources>
                    <Storyboard x:Key="mystoryboard">
                        <DoubleAnimation Storyboard.TargetName="txtSample"
                                         Storyboard.TargetProperty="Opacity"
                                         From="0"
                                         To="1"
                                         Duration="0:0:1.0">
                            <DoubleAnimation.EasingFunction>
                                <PowerEase EasingMode="EaseIn"
                                           Power="8" />
                            </DoubleAnimation.EasingFunction>
                        </DoubleAnimation>
                    </Storyboard>
                </StackPanel.Resources>
                <ListBox Name="lstSample"
                         SelectionChanged="lstSample_SelectionChanged">
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="SelectionChanged">
                            <ei:ControlStoryboardAction ControlStoryboardOption="Play"
                                                        Storyboard="{StaticResource mystoryboard}">
    
                            </ei:ControlStoryboardAction>
    
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
    
                </ListBox>
    
                <Border Name="brdrTextSampleLanguageOne"
                        BorderThickness="0">
                <TextBlock Text="This is sample text."
                           Name="txtSample"
                           TextAlignment="Right"
                           VerticalAlignment="Center" />
    
                </Border>
    
            </StackPanel>
    
        </Grid>
    </UserControl>
    

    让我知道它是否适合你。

    干杯!

    维诺德

    【讨论】:

    • 感谢您的回答。它对我不起作用。 XAML 中存在错误(在“交互”中未找到可附加属性“触发器”),即使我在引用以及 XAML 中添加了 System.Windows.Interactivity、Microsoft Expression Controls 和 Microsoft.Expression Interactions命名空间。
    • 我需要用“UserControl”替换“phone:PhoneApplicationPage”吗?
    • 是的,我已经安装了 Expression Blend 4 的完全许可版本。
    • 您还需要在 Microsoft.Expression.Interactivity 中添加引用和效果
    • 我的电脑上的任何地方都找不到这个资源
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多