首页 > 解决方案 > 为什么有些文本会在 WPF 中消失,而有些则不会

问题描述

我正在使用 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>

标签: wpfxaml

解决方案


您的非工作TextBlock目标是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"/>

推荐阅读