问题描述
I simply want to open up the WPF Popup with a delay, sort of like a ToolTip.
我该如何实现?
顺便说一句,Popup.PopupAnimation = PopupAnimation.Fade ...消失得太快.我想在那里呆至少半秒钟.
And, by the way, Popup.PopupAnimation = PopupAnimation.Fade ... fades in too quickly. I want at least half a second in there.
推荐答案
首先...该答案的功劳归于埃里克·伯克.他回答这个问题已经发布在WPF中门徒小组.我认为将这个答案也放到StackOverflow上很有用.
First off ... the credit for this answer goes to Eric Burke. He answered this very question posted in the WPF Disciples group. I thought it would be useful to put this answer out on StackOverflow too.
基本上,您需要使用DiscreteBooleanKeyFrame为弹出窗口的IsOpen属性设置动画.
检查以下xaml(可以轻松将其粘贴到 Kaxaml 或其他宽松的xaml编辑实用程序中):
Check out the following xaml (which can easily be pasted into Kaxaml or another loose xaml editing utility):
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>
<ContentPresenter>
<ContentPresenter.ContentTemplate>
<DataTemplate>
<Grid>
<CheckBox
x:Name="cb"
Width="100"
Height="40"
Content="Hover Over Me"
/>
<Popup
x:Name="popup"
Placement="Bottom"
PlacementTarget="{Binding ElementName=cb}"
>
<Border Width="400" Height="400" Background="Red"/>
</Popup>
</Grid>
<DataTemplate.Triggers>
<Trigger SourceName="cb" Property="IsMouseOver" Value="True">
<Trigger.EnterActions>
<BeginStoryboard x:Name="bsb">
<Storyboard>
<BooleanAnimationUsingKeyFrames
Storyboard.TargetName="popup"
Storyboard.TargetProperty="IsOpen"
FillBehavior="HoldEnd"
>
<DiscreteBooleanKeyFrame KeyTime="0:0:0.5" Value="True"/>
</BooleanAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
<Trigger.ExitActions>
<StopStoryboard BeginStoryboardName="bsb"/>
</Trigger.ExitActions>
</Trigger>
</DataTemplate.Triggers>
</DataTemplate>
</ContentPresenter.ContentTemplate>
</ContentPresenter>
</Page>
请注意,我稍稍修改了他的原始解决方案...以便在鼠标悬停时触发IsOpen,而不是像检查CheckBox时那样检查CheckBox.所有尝试使Popup的行为都类似于ToolTip.
这篇关于如何延迟打开WPF弹出窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!