4 using System.Collections.Generic;
8 using System.Windows.Input;
10 using SiliconStudio.Core;
11 using SiliconStudio.Presentation.Core;
12 using SiliconStudio.Presentation.Extensions;
13 using System.Windows.Data;
14 using System.Windows.Interactivity;
15 using System.Threading;
17 namespace SiliconStudio.Presentation.Behaviors
21 public KeyGesture KeyGesture
23 get {
return (KeyGesture)GetValue(KeyGestureProperty); }
24 set { SetValue(KeyGestureProperty, value); }
27 public static readonly DependencyProperty KeyGestureProperty = DependencyProperty.Register(
32 private IDisposable bindingRegistration;
36 if ((AssociatedObject is ICommandSource) ==
false)
38 var message = string.Format(
"The Host of a '{0}' must be of type '{1}'",
40 typeof(ICommandSource).FullName);
41 throw new InvalidOperationException(message);
44 SpawnCommandBindingResolutionCheckThread();
47 private void SpawnCommandBindingResolutionCheckThread()
51 ThreadPool.QueueUserWorkItem(
55 var commandSource = (ICommandSource)state;
57 Func<bool> checkFunc = () =>
59 if (commandSource.Command != null)
61 OnCommandBindingResolved();
69 if ((
bool)host.Dispatcher.Invoke(checkFunc))
77 private void OnCommandBindingResolved()
79 var rootWindow = Window.GetWindow(AssociatedObject);
80 if (rootWindow == null)
83 var localCommand = ((ICommandSource)AssociatedObject).Command;
84 var localKeyGesture = KeyGesture;
86 if (localCommand == null || localKeyGesture == null)
89 var binding =
new KeyBinding(localCommand, localKeyGesture);
91 bindingRegistration =
new AnonymousDisposable(() => rootWindow.InputBindings.Remove(binding));
92 rootWindow.InputBindings.Add(binding);
97 if (bindingRegistration != null)
99 bindingRegistration.Dispose();
100 bindingRegistration = null;
override void OnDetaching()
This class allows implementation of IDisposable using anonymous functions. The anonymous function wil...
override void OnAttached()