using System;
using System.Windows;
namespace Hardcodet.Wpf.TaskbarNotification
{
///
/// Helper class used by routed events of the
/// class.
///
internal static class RoutedEventHelper
{
#region RoutedEvent Helper Methods
///
/// A static helper method to raise a routed event on a target UIElement or ContentElement.
///
/// UIElement or ContentElement on which to raise the event
/// RoutedEventArgs to use when raising the event
internal static void RaiseEvent(DependencyObject target, RoutedEventArgs args)
{
var uiElement = target as UIElement;
if (uiElement != null)
{
uiElement.RaiseEvent(args);
}
else
{
var contentElement = target as ContentElement;
if (contentElement != null)
{
contentElement.RaiseEvent(args);
}
}
}
///
/// A static helper method that adds a handler for a routed event
/// to a target UIElement or ContentElement.
///
/// UIElement or ContentElement that listens to the event
/// Event that will be handled
/// Event handler to be added
internal static void AddHandler(DependencyObject element, RoutedEvent routedEvent, Delegate handler)
{
var uie = element as UIElement;
if (element != null && uie != null)
{
uie.AddHandler(routedEvent, handler);
}
else
{
var ce = element as ContentElement;
if (element != null && ce != null)
{
ce.AddHandler(routedEvent, handler);
}
}
}
///
/// A static helper method that removes a handler for a routed event
/// from a target UIElement or ContentElement.
///
/// UIElement or ContentElement that listens to the event
/// Event that will no longer be handled
/// Event handler to be removed
internal static void RemoveHandler(DependencyObject element, RoutedEvent routedEvent, Delegate handler)
{
var uie = element as UIElement;
if (uie != null)
{
uie.RemoveHandler(routedEvent, handler);
}
else
{
var ce = element as ContentElement;
if (ce != null)
{
ce.RemoveHandler(routedEvent, handler);
}
}
}
#endregion
}
}