为什么有些文本会在WPF中褪色,而有些不会

为什么有些文本会在WPF中褪色,而有些不会,wpf,xaml,Wpf,Xaml,我正在visual studio中使用c#创建一个WPF应用程序。我试图使文本从一种颜色淡入另一种颜色。有些文字会改变颜色,但有些只是保持单一颜色。代码似乎完全相同,所以我不知道我做错了什么 // This is the working code UserControl x:Class="MiniBiotronWPF2.Home" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

我正在visual studio中使用c#创建一个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>
//这是工作代码
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命名空间:MiniBiotronWPF2”
mc:Ignorable=“d”
d:DesignHeight=“510”d:DesignWidth=“650”>
//这是程序的其余部分,不会褪色

<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>

在第一个不起作用的示例中,请查看

Storyboard.TargetName="MySolidColorBrush1"

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

您的非工作
文本块的目标是SolidColorBrush1,它应该是MySolidColorBrush2MySolidColorBrush3

而是将样式定义为资源:

<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"/>