Skip to content

Instantly share code, notes, and snippets.

@mwisnicki
Created July 13, 2012 13:45
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mwisnicki/3104963 to your computer and use it in GitHub Desktop.
Save mwisnicki/3104963 to your computer and use it in GitHub Desktop.
[WPF] Handle Loaded event globally
using System;
using System.Windows;
namespace WpfLoadedClassHandler
{
/// <summary>
/// Shows how to globally handle Loaded event.
/// Workarounds bug: https://connect.microsoft.com/VisualStudio/feedback/details/511753/window-loadedevent-doesnt-work-properly-when-used-to-register-a-class-event-handler
/// Discussed: http://stackoverflow.com/questions/11455800/routed-event-class-handler-for-loadedevent-does-not-work-for-most-classes
/// </summary>
public partial class App : Application
{
static App()
{
// set MyInitialized=true for new windows (happens before Loaded)
EventManager.RegisterClassHandler(typeof(Window), FrameworkElement.SizeChangedEvent, new RoutedEventHandler(OnSizeChanged));
// our loaded handler
EventManager.RegisterClassHandler(typeof(UIElement), FrameworkElement.LoadedEvent, new RoutedEventHandler(OnLoaded), true);
EventManager.RegisterClassHandler(typeof(ContentElement), FrameworkContentElement.LoadedEvent, new RoutedEventHandler(OnLoaded), true);
}
private static void OnSizeChanged(object sender, RoutedEventArgs e)
{
//Console.WriteLine("SizeChanged {0}", sender);
SetMyInitialized((Window) sender, true);
}
#region MyInitialized
public static readonly DependencyProperty MyInitializedProperty =
DependencyProperty.RegisterAttached("MyInitialized", typeof (bool), typeof (App),
new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.Inherits, OnMyInitializedChanged));
private static void OnMyInitializedChanged(DependencyObject dpo, DependencyPropertyChangedEventArgs ev)
{
if ((bool)ev.NewValue == true)
{
// registering instance handler unbreaks class handlers
if (dpo is FrameworkElement)
(dpo as FrameworkElement).Loaded += EmptyRoutedEventHandler;
if (dpo is FrameworkContentElement)
(dpo as FrameworkContentElement).Loaded += EmptyRoutedEventHandler;
} else
{
throw new ArgumentException("Cannot set to false", ev.Property.Name);
}
//Console.WriteLine("MyInitialized {0} {1}=>{2}", dpo, ev.OldValue, ev.NewValue);
}
private static readonly RoutedEventHandler EmptyRoutedEventHandler = delegate { };
public static void SetMyInitialized(UIElement element, bool value)
{
element.SetValue(MyInitializedProperty, value);
}
public static bool GetMyInitialized(UIElement element)
{
return (bool) element.GetValue(MyInitializedProperty);
}
#endregion MyInitialized
private static void OnLoaded(object sender, RoutedEventArgs e)
{
Console.WriteLine("Loaded {0}", sender);
}
}
}
@warappa
Copy link

warappa commented May 6, 2016

This is awesome - works as described!

This is the only way I found so far for handling all Loaded-events globally!

Thank you very, very much!

@lindexi
Copy link

lindexi commented Jan 17, 2023

Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment