// --------------------------------------------------------------------------------------------------------------------
//
// This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License.
//
//
// The input binding trigger.
//
// --------------------------------------------------------------------------------------------------------------------
namespace HandBrakeWPF.Commands
{
using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Input;
using System.Windows.Interactivity;
///
/// The input binding trigger.
///
public class InputBindingTrigger : TriggerBase, ICommand
{
public static readonly DependencyProperty InputBindingProperty = DependencyProperty.Register("InputBinding", typeof(InputBinding), typeof(InputBindingTrigger), new UIPropertyMetadata(null));
///
/// Gets or sets the input binding.
///
public InputBinding InputBinding
{
get { return (InputBinding)GetValue(InputBindingProperty); }
set { SetValue(InputBindingProperty, value); }
}
///
/// The can execute changed.
///
public event EventHandler CanExecuteChanged = delegate { };
///
/// The can execute.
///
///
/// The parameter.
///
///
/// The .
///
public bool CanExecute(object parameter)
{
// action is anyway blocked by Caliburn at the invoke level
return true;
}
///
/// The execute.
///
///
/// The parameter.
///
public void Execute(object parameter)
{
InvokeActions(parameter);
}
///
/// The on attached.
///
protected override void OnAttached()
{
if (InputBinding != null)
{
InputBinding.Command = this;
AssociatedObject.Loaded += delegate
{
var window = GetWindow(AssociatedObject);
window.InputBindings.Add(InputBinding);
};
}
base.OnAttached();
}
///
/// The get window.
///
///
/// The framework element.
///
///
/// The .
///
private Window GetWindow(FrameworkElement frameworkElement)
{
if (frameworkElement is Window)
return frameworkElement as Window;
var parent = frameworkElement.Parent as FrameworkElement;
Debug.Assert(parent != null, "Null Parent");
return GetWindow(parent);
}
}
}