Dynamically add KeyBindings to WPF

Is it possible to dynamically determine KeyBindings based on a linked data source? I have a screen with a grid, and I allow users to save various layouts for it. I am currently associating a grid context menu with layout names (via the ViewModel), allowing them to switch with layouts through the menu.

However, I would like to associate each layout with a keyboard shortcut. Since shortcuts are user-defined, I can't just add a few <KeyBinding> elements to the XAML window. Another problem is that the binding should indicate the layout name as a parameter to the command.

Is there a way to dynamically create a series of <KeyBinding> elements from a dynamic source?

As a test, I added bindings statically to my XAML view, and they work fine, but this was only to test my concept:

 <UserControl.InputBindings> <KeyBinding Key="F7" Command="{Binding MyCommand}" CommandParameter="My Layout Name"/> <KeyBinding Key="F8" Command="{Binding MyCommand}" CommandParameter="My Other Layout Name"/> </UserControl.InputBindings> 
+4
source share
2 answers

There are several ways to do this, the easiest of which is to create a converter that will return the key, depending on the name of the command (in fact, this is a search with the layout name being the key and the key the value), It will be a little more complicated if you need handle more complex tasks, gis is a scream, and I will create a sample for it:

Here is the XAML:

 <Grid.InputBindings> <KeyBinding Key="{Binding Converter={StaticResource converter}, ConverterParameter=My Other Layout Name}" Command="{Binding MyCommand}" /> </Grid.InputBindings> 
0
source

Here is the code that I use to dynamically create key bindings as part of the fragment editor, which allows you to execute fragments by hotkey.

In addition to earlier examples, it also demonstrates how to analyze user input of key combos:

 // example key combo from user input var ksc = "Alt+Shift+M"; ksc = ksc.ToLower(); KeyBinding kb = new KeyBinding(); if (ksc.Contains("alt")) kb.Modifiers = ModifierKeys.Alt; if (ksc.Contains("shift")) kb.Modifiers |= ModifierKeys.Shift; if (ksc.Contains("ctrl") || ksc.Contains("ctl")) kb.Modifiers |= ModifierKeys.Control; string key = ksc.Replace("+", "") .Replace("-", "") .Replace("_", "") .Replace(" ", "") .Replace("alt", "") .Replace("shift", "") .Replace("ctrl", "") .Replace("ctl", ""); key = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(key); if (!string.IsNullOrEmpty(key)) { KeyConverter k = new KeyConverter(); kb.Key = (Key)k.ConvertFromString(key); } // Whatever command you need to bind to // CommandBase here is a custom class I use to create commands // with Execute/CanExecute handlers kb.Command = new CommandBase((s, e) => InsertSnippet(snippet), (s,e) => Model.IsEditorActive); Model.Window.InputBindings.Add(kb); 
0
source

Source: https://habr.com/ru/post/1339988/


All Articles