Disabling global KeyBindings in some controls in wpf c# - c#

I have a main window with some keybindings like this:
<Window.InputBindings>
<KeyBinding Command="{Binding RotateCommand}"
CommandParameter="left"
Key="L">
</KeyBinding>
<KeyBinding Command="{Binding RotateCommand}"
CommandParameter="right"
Key="R" />
</Window.InputBindings>
The problem is that I am unable to type this letters in any textboxes in that window, keys L and R just call commands.
Is there any way to disable KeyBindings inside of some controls? (Maybe when some controls have a focus).
I search through the internet and find this, but I can't get access to CanExecute routed event because I'm using mvvm model for command bindings.

Related

Handling PreviewMouseDown and PreviewMouseUp events in Prism

I have a Prism application and I'm attempting to bind PreviewMouseDown and PreviewMouseUp button events in my view to commands in my view model. When I run the code I see the following exception:
As a workaround, I'm currently binding to methods in the view and use a reference to the data context of the view model to execute the command. This works but doesn't seem correct because the view now has knowledge of the view model.
What is the proper way to handle something like this?
You cannot bind events to commands like, for example, the Command property of a button.
Luckily, you don't need to, because you have the Command property. It even disables the button if the command returns false from CanExecute.
If you have something other than a button or something other than MouseDown, you can use InvokeCommandAction (from Prism or from Interactivity)...
xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDown">
<prism:InvokeCommandAction Command="{Binding MyCommand}"/>
<!-- or -->
<i:InvokeCommandAction Command="{Binding MyCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>

WPF modal dialog on touch screen

I am using a custom modal dialog in a WPF application, that will run on a touch device (Windows 10). I am using the MVVM pattern.
My problem:
I experience different touch behavior depending on from where the dialog is opened. If it is opened from, for example, a button event handler or via button command binding, everything works fine. Then dialog buttons (typically "OK" and "Cancel") closes the dialog on first touch. But when the dialog is opened from code (not driven by button event or button command binding), the dialog buttons does not respond on the first touch. I then need to touch somewhere in the dialog before touching the button. It is kind of annoying.
I have a view model with an ExitCommand implemented that opens the dialog. Some XAML examples:
This works fine:
<Button Content="Exit" Command="{Binding ExitCommand}"/>
This does not work satisfactory:
<Label Content="Exit">
<Label.InputBindings>
<MouseBinding Gesture="LeftClick" Command="{Binding ExitCommand}"/>
</Label.InputBindings>
</Label>
And as mentioned, opening the dialog from code without any user action, does not work either.
Clues, anyone?
I'm not sure that this solution will solve your problem but try this please:
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
<Label Content="Exit">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDown">
<!-- MouseDown trigger for touch down too. -->
<i:InvokeCommandAction Command="{Binding ExitCommand}"
x:Name="exitCommandMouse" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Label>
I use a similar solution with grid (instead label) and its works perfectly with touch screen on win 8.1.

MouseBinding gesture strings in xaml

Mouse gestures can be bound to commands using the MouseBinding InputBinding,
for example:
<Grid.InputBindings>
<MouseBinding Command="{Binding MyCommand}" Gesture="LeftClick"/>
</Grid.InputBindings>
In that example, the LeftClick gesture is used. What is the full list of gesture strings? I'm looking for a left mouse button down gesture, if it exists.
That is a MouseAction value. You can see possible values in the documentation. Mouse down is not a built-in gesture. Only various clicks and double clicks are in the enumeration.
It is possible to make your own input bindings by creating classes that extend InputBinding and InputGesture. You can reference the implementation of MouseBinding for an example. Alternatively, you can find a different way to accomplish whatever it is you are trying to do.
I'm looking for a left mouse button down gesture, if it exists.
That would be the LeftClick mouse action that you are currently using.
If you want to invoke a command when the MouseLeftButtonDown event occurs, you could do this using an interaction trigger:
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseLeftButtonDown" >
<i:InvokeCommandAction Command="{Binding MyCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
Please refer to the following blog post for more information about this.
Handling events in an MVVM WPF application: https://blog.magnusmontin.net/2013/06/30/handling-events-in-an-mvvm-wpf-application/
The EventTrigger class is included in th Expression Blend SDK which you can download from here: http://www.microsoft.com/en-us/download/details.aspx?id=10801.

Keyboard shortcuts not working until I click a control in the View for MVVM

I'm having a weird problem. I binded some keyboard shortcuts like so:
BroadcastView XAML:
<UserControl.InputBindings>
<KeyBinding Key="F1" Command="{Binding StartRoundClockCmd}"/>
<KeyBinding Key="F2" Command="{Binding StopRoundClockCmd}"/>
</UserControl.InputBindings>
MainWindow XAML
<Grid>
<TabControl>
<TabItem Header="Broadcast">
<view:BroadcastView/>
</TabItem>
<TabItem Header="Options">
<view:OptionsView/>
</TabItem>
</TabControl>
</Grid>
but the problem is.. when I start the application.. F1 and F2 don't work.. i can click anywhere within the TabItem thinking I just need it to focus the tabitem.. but still nothing.. it's only when I click within a textbox.. or click a button on the View (within the TabItem) first.. THEN the keyboard shortcuts work.
how can I get it so that the keyboard shortcuts will work from application startup using MVVM?
I would use Keyboard.Focus and FocusManager if I were you.
You can set the focus on your usercontrol so it receives the keyboard shortcuts.

Override default keybindings in Listbox

I currently have a listbox in wpf c# and added some keybindings to the keydown event handler of my main window. It's event handlers for the spacebar and the up/down keys. However, when my listbox has focus, these bindings are not initiated. How can I disable these default keybindings (without entering a e.handle = true so the entire keystroke is disabled) for this control and run my own code?
The default keybindings do not use events directly but commands. What you can do is to define commands and attach events to them.
<Window.Resources>
<CommandBinding x:Key="NewBinding" Command="ApplicationCommands.New"
Executed="NewCommand" CanExecute="CanExecuteNew">
</CommandBinding>
</Window.Resources>
<ListBox.CommandBindings>
<StaticResource ResourceKey="NewBinding"></StaticResource>
</ListBox.CommandBindings>
To override default keybindings the syntax is :
<Window.InputBindings>
<KeyBinding Key="" Modifiers="" Command="" />
</Window.InputBindings>
If you are new to this try to get some overview WPF Commanding Overview and Advanced WPF you are going to have hard time to do so with events.

Categories