为什么有些文本会在WPF中淡出,而有些却不会

时间:2018-08-06 21:03:09

标签: wpf xaml

我正在使用c#在Visual Studio中创建WPF应用。我正在尝试使文本从一种颜色淡入另一种颜色。某些文本会更改颜色,但某些文本只会保留一种奇异的颜色。代码似乎完全相同,所以我不知道自己在做什么错。

 // This is the working code
UserControl x:Class="MiniBiotronWPF2.Home"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:MiniBiotronWPF2"
             mc:Ignorable="d" 
             d:DesignHeight="510" d:DesignWidth="650">
    <Grid>

        <TextBlock
  x:Name="MyChangingColorText"
  Margin="41,123,10,261" FontSize="48" FontWeight="Bold" Text="Welcome to Mini Biotron">
            <TextBlock.Foreground>
                <SolidColorBrush x:Name="MySolidColorBrush" Color="White"/>
            </TextBlock.Foreground>
            <TextBlock.Triggers>
                <EventTrigger RoutedEvent="TextBlock.Loaded">
                    <BeginStoryboard>
                        <Storyboard>
                            <ColorAnimation 
            Storyboard.TargetName="MySolidColorBrush"
            Storyboard.TargetProperty="Color"
            From="#FF383535" To="White" Duration="0:0:3"
            AutoReverse="false" RepeatBehavior="0:0:3" />
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </TextBlock.Triggers>
        </TextBlock>

        <TextBlock
  x:Name="MyChangingColorText1"
  Margin="188,220,156,168" FontSize="48" FontWeight="Bold" Text="Beta Edition">
            <TextBlock.Foreground>
                <SolidColorBrush x:Name="MySolidColorBrush1" Color="White"/>
            </TextBlock.Foreground>
            <TextBlock.Triggers>
                <EventTrigger RoutedEvent="TextBlock.Loaded">
                    <BeginStoryboard>
                        <Storyboard>
                            <ColorAnimation 
            Storyboard.TargetName="MySolidColorBrush1"
            Storyboard.TargetProperty="Color"
            From="#FF383535" To="White" Duration="0:0:3"
            AutoReverse="false" RepeatBehavior="0:0:3" />
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </TextBlock.Triggers>
        </TextBlock>

//这是程序的其余部分,不会消失

<TextBlock
  x:Name="MyChangingColorText2"
  Margin="200,385,255,0" FontSize="16" FontWeight="Bold" Text=" Created by Lane Whitten ">
            <TextBlock.Foreground>
                <SolidColorBrush x:Name="MySolidColorBrush2" Color="White"/>
            </TextBlock.Foreground>
            <TextBlock.Triggers>
                <EventTrigger RoutedEvent="TextBlock.Loaded">
                    <BeginStoryboard>
                        <Storyboard>
                            <ColorAnimation 
            Storyboard.TargetName="MySolidColorBrush1"
            Storyboard.TargetProperty="Color"
            From="#FF383535" To="White" Duration="0:0:3"
            AutoReverse="false" RepeatBehavior="0:0:3" />
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </TextBlock.Triggers>
        </TextBlock>
        <TextBlock
  x:Name="MyChangingColorText3"
  Margin="200,432,25,0" FontSize="12" FontWeight="Bold" Text=" Copyright © 2018 UW BIOTRON">
            <TextBlock.Foreground>
                <SolidColorBrush x:Name="MySolidColorBrush3" Color="White"/>
            </TextBlock.Foreground>
            <TextBlock.Triggers>
                <EventTrigger RoutedEvent="TextBlock.Loaded">
                    <BeginStoryboard>
                        <Storyboard>
                            <ColorAnimation 
            Storyboard.TargetName="MySolidColorBrush1"
            Storyboard.TargetProperty="Color"
            From="#FF383535" To="White" Duration="0:0:3"
            AutoReverse="false" RepeatBehavior="0:0:3" />
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </TextBlock.Triggers>

        </TextBlock>
        <TextBlock
  x:Name="MyChangingColorText4"
  Margin="71,409,25,0" FontSize="12" FontWeight="Bold" Text=" For questions, concerns or to request new features contact: lanewhitten14@gmail.com ">
            <TextBlock.Foreground>
                <SolidColorBrush x:Name="MySolidColorBrush4" Color="White"/>
            </TextBlock.Foreground>
            <TextBlock.Triggers>
                <EventTrigger RoutedEvent="TextBlock.Loaded">
                    <BeginStoryboard>
                        <Storyboard>
                            <ColorAnimation 
            Storyboard.TargetName="MySolidColorBrush"
            Storyboard.TargetProperty="Color"
            From="#FF383535" To="White" Duration="0:0:3"
            AutoReverse="false" RepeatBehavior="0:0:3" />
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </TextBlock.Triggers>
        </TextBlock>



    </Grid>
</UserControl>

2 个答案:

答案 0 :(得分:2)

您的无效TextBlock定位到 SolidColorBrush1 ,应该是 MySolidColorBrush2 MySolidColorBrush3 ....

改为将样式定义为资源:

<Window.Resources>
    <SolidColorBrush x:Key="TextBrush">Black</SolidColorBrush>
    <Style TargetType="TextBlock">
        <Setter Property="Foreground" Value="{StaticResource TextBrush}"/>
        <Style.Triggers>
                <EventTrigger RoutedEvent="TextBlock.Loaded">
                    <BeginStoryboard>
                        <Storyboard>
                        <ColorAnimation                                
                            Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)"
                            From="#FF383535" To="White" Duration="0:0:3"
                            AutoReverse="false" RepeatBehavior="Forever" />
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
        </Style.Triggers>
    </Style>
</Window.Resources>

然后只需在同一窗口中定义Textblock:

 <TextBlock Text="Text1"/>

答案 1 :(得分:1)

在第一个无效的示例中,请查看

Storyboard.TargetName="MySolidColorBrush1"

此Textblock元素找不到MySolidColorBrush1,因此无法正常工作。您应该将画笔放入UserControl.Resources中,以便此UserControl中的每个元素都可以找到画笔资源。