How to follow an event or find all its consumers? - c#

I defined a keybinding in my XAML-code:
<KeyBinding Command="{Binding CopyCommand}" Modifiers="Control" Key="C" />
If i press CTRL+C, the CopyCommand isn't called. If i use the following instead:
<KeyBinding Command="{Binding CopyCommand}" Modifiers="Control" Key="U" />
and press CTRL+U the command works fine.
I think the reason for this is, that the CTRL+C gets handled somewhere else and the Handled value of the KeyEventArgs is set to true, so it doesn't reach my control.
Do I have any possibility to find all consumers for this/any given event? Can I somehow debug/follow an event from the moment it is created until it is consumed?
Simply searching for KeyDown-handlers is not really an option.
I would be fine with using even an external software, e.g. something like Snoop, but would prefer a solution using Visual Studio 2012 or writing code.

Using Snoop I could see that CTRL+U gets handled by the UserControl (StructurePanelV) while CTRL+C is handled by the Devexpress` TreeListControl inside the StructurePanelView.
Because the TLC handles the PreviewKeyDown- but the KeyBinding listens on the KeyDown-event CTRL+C doesn't work (for the order of events please have a look at this article https://wpf.2000things.com/2012/08/07/619-event-sequence-for-the-key-updown-events/).
It seems there is no possibility to define a XAML-KeyBinding as a PreviewKeybinding.
The link provided by Laganimal works fine becaue it also registers on the PreviewKeyDown-event.

Related

Keybind Shortcuts - Opening other views from MainView - MVVM

I'm creating a bidding program to give customers quotes. I'm using WPF + MVVM
What I would like to do is to be able to create a shortcut (Ctrl+O) from the main page directly to the view that enables me to select which bid I want to open.
I can't imagine it is very difficult, I just seem to be missing something. I've looked for the answer in other threads. It is possible that I have missed it, if I have, please let me know and refer me to where the answer can be found.
Assuming you already have a command you are calling when clicking the button to open the other view you could use an InputBinding
Something like this should do the work
<Window>
<Window.InputBindings>
<KeyBinding Command="{Binding OpenSecondViewCommand}"
Modifiers="Control"
Key="O"/>
</Window.InputBindings>
</Window>

Applicationcommands Name in Menu Header

When using ApplicationCommands class I get a lot of things for free. Text, shortcut and localisation. However there seems to be (or I haven't found it) a way to specify the Character you would specifiy by the _ notation (e.g. E_xit). Is there a way to specify that character for instance for the
ApplicationCommands.New ?
Or is the only solution to this using a
CustomRountedUICommand
where I can simply specify _New for the name property?
EDIT:
To clarify here an extraction of my xaml file:
<MenuItem Name="NewProject" Command="{Binding MenuNewProject}" />
MenuNewProject is an ApplicationCommand.New with an InputGesture added. But how to add the underscore without setting the menu header (which is already done by the command binding)?
Edit2:
As it has been pointed out this is obviously a Menu issue. Now the question is: Is there an alternative to the _ in the text to specify the accelerator key? I haven't found anything in the MenuItem class.
Final solution:
Either use:
AccessKeyManager.Register('N',MenuItem)
but loose the shown underlined N or set
MenuItem.Header ='_New'
manually and loose the localization. Unfortunately parsing ApplicationCommands.New.Name always gives back the English name. There might be a solution with the ContentPresenter class but that is a little bit overshot for my small project.
Use an underscore _ in the Header text to create a hotkey or use a InputBinding to create a shortcut (from this answer):
<Window.CommandBindings>
<CommandBinding Command="New" Executed="CommandBinding_Executed" />
</Window.CommandBindings>
<Window.InputBindings>
<KeyBinding Key="N" Modifiers="Control" Command="New"/>
</Window.InputBindings>
RoutedUICommand's do not specify an accelerator key or shortcut key because they may be placed in multiple locations, potentially with different parameters (making them in effect different commands). Thus, the onus for applying accelerators is on the Menu which hosts the commands. You'll need to assign these yourself.
<MenuItem Header="_New"
Command="ApplicationCommands.New" />
The shortcut key you specify using an InputBinding in the XAML or in Code Behind.
<Window.InputBindings>
<KeyBinding Key="N"
Modifiers="Control"
Command="ApplicationCommands.New" />
</Window.InputBindings>
The shortcut key should show up in your menu automatically.

Bind a keyboard shortcut to a command in the view model in WPF

I have an application where I have a UserControl with a toolbar in it. This toolbar has a button Execute which in turn has its command bound to an ICommand derived class that the viewmodel exposes as a property.
<Button Grid.Row="0" Command="{Binding ExecuteCommand}">Execute</Button>
Now, I'd like to bind this to a keyboard shortcut (F5) as well. This needs to be bound in the context of the UserControl since, it's only applicable if this usercontrol is visible at the moment.
Another option is to bind it to the KeyDown of the textbox that actually contains the text to execute, but I'm really shaky when it comes to how to route the event from the control to the command in the viewmodel without really ugly hacks in the code-behind of the usercontrol.
Any pointers are appreciated!
There was another answer that disappeared for some reason. This worked fine:
<UserControl.InputBindings>
<KeyBinding Key="F5" Command="{Binding ExecuteCommand}" />
</UserControl.InputBindings>
I'd like to give credit to that guy if possible. Please appear again :)
Afaik, there isn't a way to directly bind to a keypress, but there are some work arounds. It looks like others have had this problem as well, have you seen this post? My other suggestion is to look into attached commands.

XAML - InputBindings prevent MouseLeftButtonDown-Events being fired

I have a class derived from ItemsControl in which I implement my own selection-algorithm which uses the MouseLeftButtonDown to change the selection.
Now I needed a specific control to handle Mouseclicks in the ViewModel, so I wrote the following:
<controls:DraggableItemsContainer bla="blub">
<controls:DraggableItemsContainer.InputBindings>
<MouseBinding Gesture="LeftClick" Command="{Binding DeselectSubGroupsCommand}" />
</controls:DraggableItemsContainer.InputBindings>
</controls:DraggableItemsContainer>
What happens now is, that I don't get the MouseLeftButtonDown-event anymore - which is comprehensable because the command "e.handles" the click.
But in this case, that's not what I want. Is there a way to fire the event anyway?
PS: Yes I need to do the selection in the MouseLeftButtonDown-event and not in any Preview-event
Two options:
You can have your custom ItemsControl hook PreviewMouseDown instead of MouseDown.
You can continue to hook MouseDown, but do it by calling AddHandler, and pass true for the handledEventsToo parameter.
If it's important that you get notified after the MouseBinding has done its work, then you need to use AddHandler. If you're okay with getting the message first, PreviewMouseDown is simpler.
I think your issue is that you try to catch the same event, on the same control, using two different approaches.
Try this:
<Grid>
<Grid.InputBindings>
<MouseBinding Gesture="LeftClick" Command="{Binding DeselectSubGroupsCommand}" />
</Grid.InputBindings>
<controls:DraggableItemsContainer bla="blub">
.....
</controls:DraggableItemsContainer>
</Grid>
Make sure that in your control you have e.Handled = false.
This should allow for your internal logic to run, and then execute the command. If you need it in the opposite order..... I don't know.

Handling keyboard press in WPF

I am only beginning to learn MVVM and its use in WPF.
I am using it to build a very simple Calculator application (like the Windows built in calculator).
One thing i've noticed, is that binding my view (XAML) to commands does not let me configure WHEN these actual commands are fired.
For example, a Button control fires the command bound to it when it is clicked.
I would like to achieve the same effect when the numpad buttons are presed ('1' will fire the Command of the "1" Button control, and so on).
I could not find 1 way to do this, all sites seem to show multiple other options which don't seem valid in this case.
Should this be set up in the Control's (Button) level or on the entire window? How can i do this ?
Try to set input bindings of your main window.
<Window.InputBindings>
<KeyBinding Key="D1" Command="{Binding Command1}" />
<KeyBinding Key="NumPad1" Command="{Binding Command1}" />
</Window.InputBindings>

Categories

Resources