如何将此TextBlock淡入触发器转换为样式

时间:2009-05-27 09:09:00

标签: wpf xaml styles textblock

此XAML会在文本显示时淡入淡出。

我想将此功能放入样式中。

但是,但是我为“TargetName”添加了什么,因为样式不知道哪个元素将使用它?

如何将此淡入效果转换为样式?

<TextBlock Name="Message" Text="This is a test.">
  <TextBlock.Triggers>
    <EventTrigger RoutedEvent="TextBlock.Loaded">
      <BeginStoryboard>
        <Storyboard>
          <DoubleAnimation
            Storyboard.TargetName="Message" 
            Storyboard.TargetProperty="(TextBlock.Opacity)"
            From="0.0" To="1.0" Duration="0:0:3"/>
        </Storyboard>
      </BeginStoryboard>
    </EventTrigger>
  </TextBlock.Triggers>
</TextBlock>

1 个答案:

答案 0 :(得分:5)

您不必使用TargetName。这有效:

<Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Page.Resources>
    <Style TargetType="TextBlock">
      <Style.Triggers>
        <EventTrigger RoutedEvent="TextBlock.Loaded">
          <BeginStoryboard>
            <Storyboard>
              <DoubleAnimation
                Storyboard.TargetProperty="(TextBlock.Opacity)"
                From="0.0" To="1.0" Duration="0:0:3"/>
            </Storyboard>
          </BeginStoryboard>
        </EventTrigger>
      </Style.Triggers>
    </Style>  
  </Page.Resources>
  <Grid>
    <TextBlock Name="Message" Text="This is a test.">
    </TextBlock>
  </Grid>
</Page>