WPF popup event handling - how to activate a popup

I created a WPF popup that contains a mesh with a border. There is some animation related to the border that I want to run every time a popup opens.

Currently, the code is as follows

<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> 

According to the code, animation is displayed on the border when a pop-up window opens. What change do I need to make the border animation run every time a popup opens?

+4
source share
5 answers

According to the suggestions given here, and are running out a bit now (I asked about this a year ago :)), I could find a solution.

 <Window x:Class="MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525" > <Window.Resources> <Style x:Key="popupStyle" TargetType="{x:Type Popup}" > <Style.Triggers> <Trigger Property="IsOpen" Value="True"> <Trigger.EnterActions> <BeginStoryboard> <Storyboard> <DoubleAnimation Storyboard.TargetProperty="Height" From="10" To="255" Duration="0:0:0.20" /> </Storyboard> </BeginStoryboard> </Trigger.EnterActions> </Trigger> </Style.Triggers> </Style> </Window.Resources> <Grid> <Button Width="100" Height="100" Click="Button_Click"></Button> <Popup Name="popUp" Width="100" Height="100" Style="{StaticResource popupStyle}" > <Border x:Name="myBorder" Background="Blue"/> </Popup> </Grid> 

and sample code to launch a popup.

  private void Button_Click(object sender, RoutedEventArgs e) { popUp.PlacementTarget = (Button)sender; popUp.IsOpen = true; } 

Although I can only animate Popup, not Border here, it pretty much gives the same result.

+6
source

I'm not sure if the popup focuses when it opens, but you can use the GotFocus event if that happens. Alternatively, you can try using a datatrigger on the IsOpen property. I think you will have to put this in style, but instead of inline.

+1
source

try changing the event trigger to

<EventTrigger RoutedEvent="Popup.Opened">

0
source

You can achieve this by listening to the IsOpen dependency IsOpen , for example

  public MainWindow() { InitializeComponent(); //// Listening to the IsOpen dependency property of the Popup. this.SetBinding(PopupIsOpenProperty, new Binding() { Source = this.popupContainer, Path = new PropertyPath("IsOpen") }); } /// <summary> /// Gets or sets a value indicating whether [popup is open]. /// </summary> /// <value><c>true</c> if [popup is open]; otherwise, <c>false</c>.</value> public bool PopupIsOpen { get { return (bool)GetValue(PopupIsOpenProperty); } set { SetValue(PopupIsOpenProperty, value); } } // Using a DependencyProperty as the backing store for PopupIsOpen. This enables animation, styling, binding, etc... public static readonly DependencyProperty PopupIsOpenProperty = DependencyProperty.Register("PopupIsOpen", typeof(bool), typeof(MainWindow), new PropertyMetadata(false, (dependencyObject, e) => { var mainWindow = (MainWindow)dependencyObject; if (mainWindow != null && (bool)e.NewValue == true) { //// Raise your event here... like //// mainWindow.RaisePopupOpened(); System.Diagnostics.Debug.WriteLine("Popup Open Triggered"); } })); private void button_MouseLeave(object sender, MouseEventArgs e) { this.popupContainer.IsOpen = false; } private void button_MouseMove(object sender, MouseEventArgs e) { //// Setting the popup position var p = e.GetPosition(sender as UIElement); this.popupContainer.HorizontalOffset = pX; this.popupContainer.VerticalOffset = pY; //// Enabling popup when it is hover on the button this.popupContainer.IsOpen = true; } <!-- XAML Starts here--> <Grid> <Button x:Name="button1" Content="This is a sample text" MouseMove="button_MouseMove" MouseLeave="button_MouseLeave" Width="100" Height="25" /> <Popup x:Name="popupContainer" IsHitTestVisible="False" > <Grid Background="White"> <TextBlock Text="{Binding Content, ElementName=button}" /> </Grid> </Popup> </Grid> 

NTN

0
source

In App.xaml.cs or another instance of the source class, you need to add:

 var field = typeof(PresentationSource).GetField("RootSourceProperty", BindingFlags.NonPublic | BindingFlags.Static); var property = (DependencyProperty)field.GetValue(null); property.OverrideMetadata(typeof(DependencyObject), new FrameworkPropertyMetadata(property.DefaultMetadata.DefaultValue, OnHwndSourceChanged)); 

Where RootSourceProperty is a private field of DependecyProperty of PresentationSource . Its property is used when creating an HwndSource and installing RootVisual. Therefore, you just need to use the modified RootSourceProperty callback RootSourceProperty :

 private static void OnHwndSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { } 

This is good because you can use it in your application and for all HwndSource ( Popup , Window or user controls where you use HwndSource )

0
source

All Articles