using System.Collections;
using System.Collections.Specialized;
using System.Windows;
namespace Core.Gui.Commands
{
///
/// Holds a collection of that should be
/// turned into CommandBindings.
///
public class RoutedCommandHandlers : FreezableCollection
{
private static readonly DependencyProperty commandsProperty = DependencyProperty.RegisterAttached(
"CommandsPrivate",
typeof(RoutedCommandHandlers),
typeof(RoutedCommandHandlers),
new PropertyMetadata(default(RoutedCommandHandlers)));
private readonly FrameworkElement owner;
///
/// Creates a new instance of .
///
/// The element for which this collection is created.
private RoutedCommandHandlers(FrameworkElement owner)
{
this.owner = owner;
((INotifyCollectionChanged) this).CollectionChanged += OnCollectionChanged;
}
///
/// Gets the collection of RoutedCommandHandler for a given element, creating
/// it if it doesn't already exist.
///
/// The element to which
/// was added.
public static RoutedCommandHandlers GetCommands(FrameworkElement element)
{
var handlers = (RoutedCommandHandlers) element.GetValue(commandsProperty);
if (handlers == null)
{
handlers = new RoutedCommandHandlers(element);
element.SetValue(commandsProperty, handlers);
}
return handlers;
}
protected override Freezable CreateInstanceCore()
{
return new RoutedCommandHandlers(owner);
}
private static void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs args)
{
((RoutedCommandHandlers) sender).OnAdd(args.NewItems);
}
private void OnAdd(IEnumerable newItems)
{
if (newItems == null)
{
return;
}
foreach (RoutedCommandHandler routedHandler in newItems)
{
routedHandler.Register(owner);
}
}
}
}