动画在ItemsControl中不起作用

时间:2014-05-27 13:10:35

标签: c# wpf xaml wpf-animation

我正在尝试构建一个行为类似于文本块的用户控件,但在文本更改时执行动画。我已成功创建了这样一个文本块,但我无法将此功能嵌入到自己的控件中。这是我目前的尝试:

<ItemsControl Name="_itemsControl">
  <ItemsControl.ItemTemplate>
    <DataTemplate>
      <TextBlock Text="{Binding ., NotifyOnTargetUpdated=True}">
        <TextBlock.Background>
          <SolidColorBrush x:Name="bgBrush"></SolidColorBrush>
        </TextBlock.Background>

        <TextBlock.Triggers>
          <EventTrigger RoutedEvent="Binding.TargetUpdated">
            <BeginStoryboard>
              <Storyboard>
                <ColorAnimation 
                  Storyboard.TargetName="bgBrush"
                  Storyboard.TargetProperty="Color" To="Red" Duration="0:0:.5"
                  AutoReverse="True"/>
              </Storyboard>
            </BeginStoryboard>
          </EventTrigger>
        </TextBlock.Triggers>
      </TextBlock>
    </DataTemplate>
  </ItemsControl.ItemTemplate>

  <ItemsControl.ItemsPanel>
    <ItemsPanelTemplate>
      <StackPanel Orientation="Horizontal" />
    </ItemsPanelTemplate>
  </ItemsControl.ItemsPanel>
</ItemsControl>

后面的代码只是声明了Text依赖项属性,并根据this answer在发送OnPropertyChanged事件时将控件的ItemsSource属性设置为此属性。

当我将此控件的Text属性绑定到我自己的应用程序内的属性时,它会适当地注册内容更改,但它不会显示动画。但是,如果我将Textblock放在上面的代码段中并将其粘贴到我的应用程序中(修复当然的绑定),则会显示动画。我在这里缺少什么?

0 个答案:

没有答案
相关问题