C# WPF TextBlock 可见性在 StoryBoard 之后没有改变

Posted

技术标签:

【中文标题】C# WPF TextBlock 可见性在 StoryBoard 之后没有改变【英文标题】:C# WPF TextBlock Visibility doesn't change after StoryBoard 【发布时间】:2019-02-25 22:47:52 【问题描述】:

我有一个带有按钮的 WPF 应用程序,单击该按钮时会执行一个函数,该函数将 TextBlock 的可见属性变为可见:

        savetxt.Visibility = Visibility.Visible;
        System.Windows.MessageBox.Show(savetxt.Visibility.ToString());

txt.Visibility.ToString()); 当 TextBlock 变为可见时,一个使其淡入淡出的情节提要开始:

<TextBlock x:Name="savetxt" Visibility="Hidden" Text="Hello">
    <TextBlock.Style>
        <Style TargetType="TextBlock">
            <Style.Triggers>
                <Trigger Property="Visibility" Value="Visible">
                    <Trigger.EnterActions>
                        <BeginStoryboard>
                            <Storyboard>
                                <ObjectAnimationUsingKeyFrames BeginTime="0:0:0" Storyboard.TargetProperty="Visibility">
                                    <DiscreteObjectKeyFrame KeyTime="0">
                                        <DiscreteObjectKeyFrame.Value>
                                            <Visibility>Visible</Visibility>
                                        </DiscreteObjectKeyFrame.Value>
                                    </DiscreteObjectKeyFrame>
                                </ObjectAnimationUsingKeyFrames>
                                <DoubleAnimation BeginTime="0:0:0.0" Storyboard.TargetProperty="Opacity" From="0" To="1" Duration="0:0:0.2"/>
                                <DoubleAnimation BeginTime="0:0:5.0" Storyboard.TargetProperty="Opacity" From="1" To="0" Duration="0:0:0.5"/>
                                <ObjectAnimationUsingKeyFrames BeginTime="0:0:5.5" Storyboard.TargetProperty="Visibility">
                                    <DiscreteObjectKeyFrame KeyTime="0">
                                        <DiscreteObjectKeyFrame.Value>
                                            <Visibility>Hidden</Visibility>
                                        </DiscreteObjectKeyFrame.Value>
                                    </DiscreteObjectKeyFrame>
                                </ObjectAnimationUsingKeyFrames>
                            </Storyboard>
                        </BeginStoryboard>
                    </Trigger.EnterActions>
                </Trigger>
            </Style.Triggers>
        </Style>
    </TextBlock.Style>
</TextBlock>

第一次按下按钮时,MessageBox.Show 显示 Visible,故事板开始成功制作动画。但是,当我第二次按下该按钮时,MessageBox.Show 显示 Hidden 并且没有其他任何反应。 谁能帮我弄清楚为什么 savetxt.Visibility = Visibility.Visible;第一次后不改变可见性?

【问题讨论】:

【参考方案1】:

您可以从代码中设置故事板。

xaml

 <TextBlock Grid.Row="0" x:Name="savetxt" Visibility="Hidden" Text="Hello"></TextBlock>
        <Button Grid.Row="1"  Content="Test" Height="25" Width="75" Click="Button_Click"></Button>   

代码

 private void Button_Click(object sender, RoutedEventArgs e)
        
            savetxt.Visibility = Visibility.Visible;

            Storyboard storyboard = new Storyboard();
            TimeSpan duration = new TimeSpan(0, 0, 5);
            DoubleAnimation animation = new DoubleAnimation();

            animation.From = 1.0;
            animation.To = 0.0;
            animation.Duration = new Duration(duration);

            Storyboard.SetTargetName(animation, savetxt.Name);
            Storyboard.SetTargetProperty(animation, new PropertyPath(OpacityProperty));
            // Add the animation to the storyboard
            storyboard.Children.Add(animation);

            // Begin the storyboard
            storyboard.Begin(this);

            MessageBox.Show(savetxt.Visibility.ToString());
                

【讨论】:

以上是关于C# WPF TextBlock 可见性在 StoryBoard 之后没有改变的主要内容,如果未能解决你的问题,请参考以下文章

c# wpf 多个textblock滚动字幕怎么实现?

TextBlock在wpf C#中的图像内的特定坐标中设置

WPF(C#)中如何让数据库数据逐个显示在textblock中?

C#中wpf中textblock写了一个动态时间,为啥显示的时候一直延续,而不是刷新原来的?

求C#代码,控制WPF控件颜色变化

WPF:通过字符串内容绑定可见性