WPF淡出上的控件 [英] WPF Fade Out on a control
问题描述
在我的WPF应用程序,我有希望出现一个用户操作完成后(保存数据,删除...)反馈控制。可见性被设置为隐藏的开始和样式设置为限定为一个资源的animateFadeOut式(见下文)。然后我想设置文本和控制能见度可见在我的C#code和具有反馈控制显示的消息,5秒后淡出,并保持隐藏(Visibility.Hidden)。
下面的XAML的工作,我第一次打电话control.Visiblity = Visibility.Visible但控制没有再出现第二次。我想,这是因为动画仍然在运行,其中有超过反馈控制的控制。然后我试图设置FillBehavior停止,但刚才的控制再次可见,我想它隐藏起来。然后,FillBehavior =停止,我试图设置一个触发时,不透明度= 0时,设置能见度隐藏。触发似乎没有火灾和我留下了可见控制动画完成一次后。
请帮忙指出我在做什么错在这里。
另外,如果你能提出一个更好的方式来显示5秒后变淡,并可以反复被称为控制,我会AP preciate它。
谢谢!
<风格的TargetType ={X:类型FrameworkElement的}X:键=animateFadeOut>
< Style.Triggers>
<触发属性=能见度VALUE =可见>
< Trigger.EnterActions>
< BeginStoryboard>
<情节提要>
< DoubleAnimation是的BeginTime =0:0:5.0Storyboard.TargetProperty =透明度
从=1.0为了=0.0时长=0:0:0.5/>
< /故事板>
< / BeginStoryboard>
< /Trigger.EnterActions>
< /触发>
< /Style.Triggers>
< /样式和GT;
块引用>解决方案的问题是,你的动画完成你的控制后,仍然有能见度=可见的,所以它不能被重新输入。结果
我宁愿用动画,做整个事情,首先显示了控制,然后隐藏它。<情节提要X:键=动画>
< ObjectAnimationUsingKeyFrames的BeginTime =0:0:0Storyboard.TargetProperty =能见度>
&所述; DiscreteObjectKeyFrame KeyTime =0>
< DiscreteObjectKeyFrame.Value>
<能见度和GT;可见的LT; /能见度和GT;
< /DiscreteObjectKeyFrame.Value>
< / DiscreteObjectKeyFrame>
< / ObjectAnimationUsingKeyFrames>
&所述; DoubleAnimation是的BeginTime =0:0:0.0Storyboard.TargetProperty =透明度从=0要=1持续时间=0:0:0.2/>
&所述; DoubleAnimation是的BeginTime =0:0:5.0Storyboard.TargetProperty =透明度从=1要=0时长=0:0:0.5/>
< ObjectAnimationUsingKeyFrames的BeginTime =0:0:5.5Storyboard.TargetProperty =能见度>
&所述; DiscreteObjectKeyFrame KeyTime =0>
< DiscreteObjectKeyFrame.Value>
<能见度和GT;隐藏< /能见度和GT;
< /DiscreteObjectKeyFrame.Value>
< / DiscreteObjectKeyFrame>
< / ObjectAnimationUsingKeyFrames>
< /故事板>如下:
和使用它:
((故事板)FindResource(动画))开始(someControl)。
In my WPF app, I have a feedback control that I want to appear after a user action completes (save data, delete...). The visibility is set to Hidden to begin and style set to the animateFadeOut style defined as a resource (see below). Then I want to set the text and control Visibility to visible in my C# code and have the feedback control display the message and fade out after 5 seconds and remain hidden (Visibility.Hidden).
The following XAML works the first time I call control.Visiblity= Visibility.Visible but the control doesn't reappear the second time. I figure that is because the animation is still running, which has control over the feedback control. I then tried to set FillBehavior to "Stop" but that just made the control visible again and I want it hidden. Then, with FillBehavior="Stop", I tried to set a trigger "when Opacity = 0, set the Visibility to Hidden". The trigger didn't seem to fire and I was left with the visible control once more after the animation completed.
Please help point out what I am doing wrong here.
Alternatively, if you can suggest a better way to display a control that fades after 5 seconds and can be called over and over, I would appreciate it.
Thanks!
<Style TargetType="{x:Type FrameworkElement}" x:Key="animateFadeOut"> <Style.Triggers> <Trigger Property="Visibility" Value="Visible"> <Trigger.EnterActions> <BeginStoryboard > <Storyboard> <DoubleAnimation BeginTime="0:0:5.0" Storyboard.TargetProperty="Opacity" From="1.0" To="0.0" Duration="0:0:0.5"/> </Storyboard> </BeginStoryboard> </Trigger.EnterActions> </Trigger> </Style.Triggers> </Style>
解决方案The problem is that after your animation completes your control still has Visibility=Visible, so it cannot be entered again.
I would rather use animation that does the whole thing, first shows the control, then hides it.<Storyboard x:Key="animate"> <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>
And use it as follows:
((Storyboard)FindResource("animate")).Begin(someControl);
这篇关于WPF淡出上的控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!