How to bind keyboard keys in button WPF? - wpf

I am working on a calculator App on WPF.
I would like to ask How can I capture when the user press a key in keyboard?
Currently, This is my code I am working with. But when I test it, it does not work.
<Button x:Name="cmdKeyEqual" Command="{Binding cmdEqual}" IsDefault="{Binding ElementName=cmdKeyEqual}" HorizontalAlignment="Left" Height="113" Margin="290,376,0,0" VerticalAlignment="Top" Width="65" Style="{StaticResource SimpleButtonStyle}" RenderTransformOrigin="0.332,0.33">
<KeyBinding
Key="Return"
Command="{Binding cmdEqual}"
Modifiers="Control"
/>
</Button>
Additionaly, the content of the button is 'System.Windows.Input.Keybinding'
Thanks in Advance. Kind Regards

Put the KeyBinding on your Window's or UserControl's InputBindings, so that when the window/control is in focus, and you press the key combination Ctrl+Return, it will execute the command binding.
<Window ...>
<Window.InputBindings>
<KeyBinding Key="Return" Command="{Binding cmdEqual}" Modifiers="Control" />
</Window.InputBindings>
...
</Window>

Related

How to fire double click event in a combobox in a child user control?

I have a user control that use another user control. The child user control has a combobox and the click event works because i can open the combobox, but the double click it doesn't work.
My code is this:
Main user control:
<StackPanel>
<views:ucMyChildUserControl/>
</StackPanel>
My child user control:
<StackPanelOrientation="Horizontal">
<StackPanel Orientation="Vertical">
<Label Content="Content" Style="{StaticResource LabelDefault}"/>
<ComboBox Name="cmbMyCombobox"/>
</StackPanel>
<!--More related controls-->
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDoubleClick">
<i:InvokeCommandAction Command="{Binding MyCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</StackPanel>
But I have realized that if the comand of mouse double click is set in the parent user control, it works:
<views:ucChildUserControl>
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDoubleClick">
<i:InvokeCommandAction Command="{Binding MyCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</views:ucChildUserControl>
So I guess the problem is about the handling of the event, but I don't know how to catch it in the child user control.
Thanks.
The issue is that the MouseDoubleClick event and the PreviewMouseDoubleClick are defined on Control. Since UserControl is a derivative of Control, the event is available and can be handled. However, StackPanel is not a derivative of Control so the event is not available, therefore the trigger does not work.
There are workarounds to this in code-behind that you can eventually turn into a behavior for MVVM, but simple workarounds like using input bindings on the left mouse click action only work on some elements, as others like ComboBox will already handle it and then the input bindings are not triggered.
<StackPanel Orientation="Horizontal">
<StackPanel.InputBindings>
<MouseBinding MouseAction="LeftDoubleClick"
Command="{Binding MyCommand}"/>
</StackPanel.InputBindings>
<StackPanel Orientation="Vertical">
<Label Content="Content"/>
<ComboBox Name="cmbMyCombobox"/>
</StackPanel>
<!--More related controls-->
</StackPanel>
The most simple solution without creating additional code is to wrap the StackPanel in a control that is a derivative of Control, e.g. a ContentControl, like this.
<ContentControl>
<StackPanel Orientation="Horizontal">
<StackPanel Orientation="Vertical">
<Label Content="Content"/>
<ComboBox Name="cmbMyCombobox"/>
</StackPanel>
<!--More related controls-->
</StackPanel>
<b:Interaction.Triggers>
<b:EventTrigger EventName="MouseDoubleClick">
<b:InvokeCommandAction Command="{Binding MyCommand}" />
</b:EventTrigger>
</b:Interaction.Triggers>
</ContentControl>

How to pass textbox KeyEventArgs and textbox value in command wpf

I am struggling with a thing like i have a textbox
<TextBox Name="FilterInputText" Visibility="{Binding VisibiltyAttr}" Width="500" Height="30" Text="{Binding InputText}" HorizontalAlignment="Left" Margin="5">
<TextBox.InputBindings>
<KeyBinding Command="{Binding EnterCommand}" Key="Enter" />
<KeyBinding Command="{Binding DownSelectionCommand}" Key="Down" />
</TextBox.InputBindings>
<i:Interaction.Triggers>
<i:EventTrigger EventName="KeyUp">
<i:InvokeCommandAction Command="{Binding CompleteCommand}" CommandParameter="{Binding Text, ElementName=InputText}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBox>
i have a event triggered here with something input inside textbox with "KeyUp" event. now i am working for autocomplete textbox so i showed suggestions on typing in a list box that is working fine. so i need to bind down key to bind with this text box so that after showing suggestions user can press down key and select his desired option from there.
It will work fine for keybinding for down key.
The problem is with event keyup because any key press inside textbox then this event triggerd. now i am sending textbox value as command parmeter but i also need to send keyeventargs with comamnd pararmter so that i can findout which key is pressed and when downkey comesup i will not further execute the method.
So how i can i pass both textbox value and keyeventargs as command parameter, i am strictly following mvvm pattern.
By default, InvokeCommandAction passes the event args if you do not specify a command parameter. Also, you're textbox is bound to a property on your view model. So, if you change the the code as follows:
<TextBox Name="FilterInputText" Visibility="{Binding VisibiltyAttr}" Width="500" Height="30" Text="{Binding InputText, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" Margin="5">
<TextBox.InputBindings>
<KeyBinding Command="{Binding EnterCommand}" Key="Enter" />
<KeyBinding Command="{Binding DownSelectionCommand}" Key="Down" />
</TextBox.InputBindings>
<i:Interaction.Triggers>
<i:EventTrigger EventName="KeyUp">
<i:InvokeCommandAction Command="{Binding CompleteCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBox>
And then make sure your command's execute method has the right event args type for a parameter, then when the CompleteCommand is invoked you should have the event args as the parameter, and you should be able to check the InputText property on your view model for the text value.
Notice that I added "UpdateSourceTrigger=PropertyChanged" to the binding on the TextBox Text property. That will cause the property to be updated in the view model every single time the user types.
See the following link for my source on InvokeCommandAction's default behavior:
https://github.com/Microsoft/XamlBehaviors/issues/126

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>

Resources