/// <summary> /// Retrieves the mapping collection /// </summary> /// <param name="obj"></param> /// <returns></returns> public static InputBinderCollection GetMappings(DependencyObject obj) { var map = obj.GetValue(MappingsProperty) as InputBinderCollection; if (map == null) { map = new InputBinderCollection(); SetMappings(obj, map); } return(map); }
/// <summary> /// This changes the event mapping /// </summary> /// <param name="target"></param> /// <param name="e"></param> private static void OnMappingsChanged(DependencyObject target, DependencyPropertyChangedEventArgs e) { if (Designer.InDesignMode) { return; } var sender = target as Page; if (sender != null) { _activePage = sender; _activePage.Unloaded += SenderOnUnloaded; if (e.NewValue != null) { Window.Current.Dispatcher.AcceleratorKeyActivated += DispatcherOnAcceleratorKeyActivated; Window.Current.CoreWindow.Activated += CoreWindowOnActivated; InputBinderCollection bindingCollection = (InputBinderCollection)e.NewValue; // Forward the data context so bindings work properly. foreach (var entry in bindingCollection) { entry.SetBinding(FrameworkElement.DataContextProperty, new Binding { Source = _activePage, Path = new PropertyPath("DataContext") }); } bindingCollection.CollectionChanged += OnForwardDataContext; } else { Window.Current.Dispatcher.AcceleratorKeyActivated -= DispatcherOnAcceleratorKeyActivated; Window.Current.CoreWindow.Activated -= CoreWindowOnActivated; ActiveKeys.Clear(); } if (e.OldValue != null) { InputBinderCollection bindingCollection = (InputBinderCollection)e.OldValue; bindingCollection.CollectionChanged -= OnForwardDataContext; } } }
/// <summary> /// Checks for any input binding match in the current keyboard state. /// If we find one, run the associated command. /// </summary> private static void CheckForActiveKey() { if (_activePage == null || ActiveKeys.Count == 0) { return; } InputBinderCollection bindingCollection = GetMappings(_activePage); if (bindingCollection == null || bindingCollection.Count == 0) { return; } foreach (var binding in bindingCollection) { if (binding.CheckKey(ActiveKeys)) { // If the currently active element is some form of TextBox, then // shift focus so that any data bound information is transferred properly Control currentFocusedControl = FocusManager.GetFocusedElement() as Control; if (currentFocusedControl is TextBox || currentFocusedControl is RichEditBox || currentFocusedControl is PasswordBox) { Frame frameOwner = Window.Current.Content as Frame; if (frameOwner != null) { if (frameOwner.Focus(FocusState.Programmatic)) { currentFocusedControl.Focus(FocusState.Programmatic); } } } ICommand command = binding.Command; if (command != null && command.CanExecute(binding.CommandParameter)) { command.Execute(binding.CommandParameter); } } } }
/// <summary> /// This sets the mapping collection. /// </summary> /// <param name="obj">Dependency Object</param> /// <param name="value">Mapping collection</param> public static void SetMappings(DependencyObject obj, InputBinderCollection value) { obj.SetValue(MappingsProperty, value); }