我有一个带有按钮的WPF应用程序,单击该按钮即可执行将TextBlock的visible属性变为Visible的功能:
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显示为“隐藏”,并且没有其他任何反应。 谁能帮助我找出为什么savetxt.Visibility = Visibility.Visible;第一次后不改变可见性?
答案 0 :(得分:1)
您可以通过代码设置情节提要。
<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());
}