How do I create a XAML KeyBinding for CTRL+' - wpf

In a WPF MVVM application I need to trigger a command when CTRL+' (control and apostrophe) is pressed. None of the following will compile...
<KeyBinding Modifiers="CTRL" Key="'" Command="{Binding MyCommand}"/>
<KeyBinding Modifiers="CTRL" Key="\'" Command="{Binding MyCommand}"/>
<KeyBinding Modifiers="CTRL" Key="&apos;" Command="{Binding MyCommand}"/>
<KeyBinding Gesture="CTRL+'" Command="{Binding MyCommand}"/>
<KeyBinding Gesture="CTRL+&apos;" Command="{Binding MyCommand}"/>
<KeyBinding Gesture="CTRL+\'" Command="{Binding MyCommand}"/>
So how can this key combination be achieved?

An apostrophe is not a key. It's a character that is eventually mapped to a key depending on the input device. OemQuestion works on my keyboard:
<KeyBinding Modifiers="CTRL" Key="OemQuestion" Command="{Binding MyCommand}"/>
...but you may be better off handling the PreviewTextInput event if you really want to detect when an apostrophe is typed in:
How to detect when (forward) slash key is pressed in OEM keys C#

The key code for apostrophe is OemQuotes. This syntax should do the trick
<KeyBinding Command="{Binding Command}"
Modifiers="CTRL" Key="OemQuotes" />

Related

In WPF, how to check/uncheck a checkbox on 1/0 or y/n entered by user

I have a checkbox in a user control. I want the user to be able to check the checkbox when he enters y or 1, and to uncheck it when n or 0 is entered.
Better to have it all in XAML if possible.
Thank you in advance, your help is much appreciated.
You can add a KeyBinding to the UserControl to respond to the key presses in XAML, but you would still need to write a Command that changes the binding property.
<UserControl.InputBindings>
<KeyBinding Key="Y" Command="{Binding MyCommand}" />
<KeyBinding Key="N" Command="{Binding MyCommand}" />
<KeyBinding Key="D0" Command="{Binding MyCommand}" />
<KeyBinding Key="D1" Command="{Binding MyCommand}" />
</UserControl.InputBindings>

Multiple Keys in KeyBinding?

I want to setup hotkeys in my Wpf application.
Using KeyBindings works in the simple case, for example: Ctrl+B, Ctrl+A, etc
<KeyBinding Gesture="Ctrl+B" Command="{Binding Source={StaticResource localCusomCommands}, Path=AddTTRowCommand}" ></KeyBinding>
<KeyBinding Modifiers="Control" Key="B" Command="{Binding Source={StaticResource localCusomCommands}, Path=AddTTRowCommand}" ></KeyBinding>
But if I need hotkeys with multiple keys Ctrl+P+B, Ctrl+A+B, etc
<KeyBinding Modifiers="Control" Key="P,B" Command="{Binding Source={StaticResource localCusomCommands}, Path=AddTTRowCommand}" ></KeyBinding>
Don't work.
<KeyBinding Gesture="Ctrl+B+A" Command="{Binding Source={StaticResource localCusomCommands}, Path=AddTTRowCommand}" ></KeyBinding>
Don't compile. Error 15 Unrecognized ModifierKeys 'B'.
How to achieve this?
As i told, there is implementation for overriding the KeyBinding and Gestures for multiple keys. refer the below link. He has overriden the implemetation of InputGesture to achieve this multikeybinding.
http://kent-boogaart.com/blog/multikeygesture

How to use EventTrigger and InvokeCommandAction programmatically?

I have a popup with a TextBox that the user should enter a ticket number into, and then when the user presses the enter key I want the ticket number to be passed to the ViewModel which will retrieve the ticket.
Here's the xaml for the TextBox:
<TextBox x:Name="TicketNumber">
<i:Interaction.Triggers>
<i:EventTrigger EventName="KeyDown">
<i:InvokeCommandAction Command="{Binding OpenTicketCommand}"
CommandParameter="{Binding ElementName=TicketNumber,
Path=Text}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBox>
The above works on any keypress, but I really only want it to happen when the enter key is pressed. How would I go about doing that?
EDIT: I am assuming it would have to be done programmatically (hence the title), but if not that's okay too.
You could use InputBindings - KeyBinding as alternative approach.
Something like this:
<TextBox x:Name="TicketNumber">
<TextBox.InputBindings>
<KeyBinding Key="Enter"
Command="{Binding OpenTicketCommand}"
CommandParameter="{Binding ElementName=TicketNumber,
Path=Text}"/>
</TextBox.InputBindings>
</TextBox>

DataGrid Multiple KeyBinding to same function, how to know which key i have pressed

I have this xaml:
<DataGrid.InputBindings>
<KeyBinding Key="a" Command="{Binding Path=Function}" CommandParameter="{Binding ElementName=dataGrid1,Path=SelectedItem}"></KeyBinding>
<KeyBinding Key="s" Command="{Binding Path=Function}" CommandParameter="{Binding ElementName=dataGrid1,Path=SelectedItem}"></KeyBinding>
</DataGrid.InputBindings>
When i select a row and press a or s i bind the selected item to KeyPressed. My problem is how can i know which key i have pressed ? (I want to bind key pressed and selected item)
If you have 2 keybindings, it would be more clear to have 2 distinct commands :
<DataGrid.InputBindings>
<KeyBinding Key="a" Command="{Binding Path=ACommand}" CommandParameter="{Binding ElementName=dataGrid1,Path=SelectedItem}"></KeyBinding>
<KeyBinding Key="s" Command="{Binding Path=SCommand}" CommandParameter="{Binding ElementName=dataGrid1,Path=SelectedItem}"></KeyBinding>
</DataGrid.InputBindings>

Key binding to '?'

I'm trying to add a keybinding onto a wpf datagrid when the user enters a '?' and have tried the following:
<DataGrid.InputBindings>
<KeyBinding Command="{Binding Path=OpenPrompt}" CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGrid}, Path=CurrentCell}" Key="OemBackslash" Modifiers="Shift"/>
</DataGrid.InputBindings>
i've also tried having the key set to 'Backslash' and 'Divide'.
Does anyone know how to do this?
Acordding to Key Enumeration it's have to be OemQuestion
I tired it and it works:
<DataGrid.InputBindings>
<KeyBinding Command="{Binding Path=MyCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGrid}, Path=CurrentCell}" Key="OemQuestion" Modifiers="Shift"/>
</DataGrid.InputBindings>
Perhaps you have a Binding problem?

Resources