WPF Popup 事件处理 - 如何在 Popup 打开时触发
我创建了一个 WPF 弹出窗口,其中包含带边框的网格。 有一些与边框相关的动画,我希望每次弹出窗口打开时都会触发这些动画。
目前的代码是这样的
<Popup x:Name="myPopUp" >
<Border x:Name="myBorder" >
<Border.Triggers>
<EventTrigger RoutedEvent="Popup.Loaded">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation
Storyboard.TargetName="myBorder"
Storyboard.TargetProperty="Height"
From="10" To="255" Duration="0:0:0.20" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Border.Triggers>
<Grid />
</Border>
</Popup>
根据代码,边框在弹出窗口第一次打开时显示动画。 每次弹出窗口打开时,我需要进行哪些更改才能触发边框动画?
I created a WPF Popup which contains a grid with border.
There is some animation associated with the border which I want to be triggered every time the Popup opens.
Currently the code is like this
<Popup x:Name="myPopUp" >
<Border x:Name="myBorder" >
<Border.Triggers>
<EventTrigger RoutedEvent="Popup.Loaded">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation
Storyboard.TargetName="myBorder"
Storyboard.TargetProperty="Height"
From="10" To="255" Duration="0:0:0.20" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Border.Triggers>
<Grid />
</Border>
</Popup>
As per the code the border shows up the animation for the first time the popup opens.
What change do I need to make to trigger the border animation every time the Popup opens?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我不确定弹出窗口在打开时是否获得焦点,但如果确实如此,您可以使用 GotFocus 事件。或者,您可以尝试在 IsOpen 属性上使用数据触发器。我认为你必须将其放在一种样式中,而不是内联。
I'm not sure if the popup gets focus when it opens, but you could use the GotFocus event if it does. Alternatively, you could try using a datatrigger on the is IsOpen property. I think you'd have to put that in a style though instead of inline.
您可以通过监听
IsOpen
依赖属性(如HTH)来实现此目的
You can achieve this by listening to the
IsOpen
dependency property likeHTH
在 App.xaml.cs 或另一个起始类实例中,您需要添加:
其中,
RootSourceProperty
是PresentationSource
的私有字段DependecyProperty
。它的属性在创建 HwndSource 并设置 RootVisual 时使用。因此,您只需使用RootSourceProperty
的属性更改回调:这很好,因为您可以在所有应用程序和所有 HwndSource (
Popup
,Window) 中使用它
或自定义控件,您在其中使用HwndSource
)In App.xaml.cs or in another starting class instance you need add:
Where,
RootSourceProperty
is private fieldDependecyProperty
ofPresentationSource
. Its property use when HwndSource is created and set RootVisual. So you need just use property changed call back ofRootSourceProperty
:This is nice because, you can use it in your all Application and for all HwndSource (
Popup
,Window
or Custom controls, where you are usingHwndSource
)尝试将事件触发器更改为
try changing your event trigger to
<EventTrigger RoutedEvent="Popup.Opened">
根据这里给出的建议和现在有点过期(我一年前问过这个:)),我可以找到解决方案。
后面还有一个触发弹出窗口的示例代码。
虽然我只能为弹出窗口设置动画,而不能为边框设置动画,但它几乎给出了相同的结果。
As per suggestions given here and a little bit expireince now (I asked this a year back :) ), I could figure out the solution.
and a sample code behind to trigger the popup..
Although I can only animate the Popup and not the Border here, it pretty much gives the same result.