wpf label, access key, event - wpf

When using a WPF label as a keyboard shortcut mechanism, is there an event fired for this?

and 2 minutes later I actually find this:
AccessKeyManager.AccessKeyPressed

Related

How do I find out where the focus is going in my WPF application?

I have a search screen in my WPF application. The screen is implemented as a UserControl in a TabItem of a TabControl. When the user switches to the Search tab, I want the focus to go into one particular field.
So I added a Loaded event handler to the UserControl tag in the Xaml and I called the Focus method of the control I want to have the initial focus in the Loaded event handler. This worked great until I upgraded the Telerik control library I'm using today. Now, when I switch to the Search tab, the focus is NOT in the field I want to have it, but I can't tell what control does have the focus.
The field I want to have focus already has GotFocus & LostFocus event handlers for other reasons. I remembered that in Win Forms, the LostFocus event handler arguments tell you which control is going to get the focus. So I put a breakpoint in my LostFocus handler & discovered that the arguments to the LostFocus event handler in WPF don't include that information.
How can I figure out where the focus is going without putting GotFocus handlers on every control in my UserControl?
Tony
You can try putting your breakpoint on the LostKeyboardFocus Attached Event instead of the LostFocus Event. It uses the KeyboardFocusChangedEventArgs Class which does have properties that show which element had focus and where the focus is going.
private void textBox1_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
textBox1.Text = ((FrameworkElement)e.NewFocus).Name ;
}
Try to press Tab Key and see if it helps you find the control in focus.
You can also use Snoop as suggested in this Q/A: Any tips on debugging focus issues in WPF?
For starters, Snoop shows the current focused element and the current
FocusScope in the status bar.
You can get it to show you all the GotFocus and LostFocus events:
1. Run your app.
2. Run Snoop.
3. Choose your app in the dropdown.
4. Click the binoculars ("Snoop") button.
5. On the right pane, click the Events tab.
6. Click to bring down the dropdown.
7. Scroll down to the Keyboard section and check GotKeyboardFocus, LostKeyboardFocus, and optionally the PreviewXXX events.
8. Now do what you need to do to manipulate focus and watch the Snoop window.
Similarly you can track the FocusManager events the same way.

Why won't the form (or control) keydown event fire when a form with a control in it has focus?

I just have a form, and using this.Controls.Add in the form I have added a container control which basically fills the whole background area of the form (and contains many other controls like datagridviews, comboboxes etc), so I can't click off of it.
Now, in the form class, I want to add some keyboard shortcuts.. Like, F5 saves my work for instance. Anyway, I have hooked up to the control's keydown even in the form class, but, it doesn't seem to fire!
Can anyone tell me why?
Thanks,
Isaac
It would probably have helped if you would have described which control you were adding. You are most likely adding a control that is trying to read the keyboard events. For the form to still get those events, change this property:
this.KeyPreview = True;

Fire event when WPF button was pressed/unpressed

I need to fire some event when WPF button is pressed (by mouse, keyboard, touchscreen, etc) and to fire event when WPF buttons is unpressed.
How to do this? It should be easy but I can't find how to do this.
You can derive from Button and override the OnIsPressedChanged method and fire a custom event there.
Or you can bind to the ButtonBase.IsPressed property.
Another option is to use DependencyPropertyDescriptor:
var descriptor = DependencyPropertyDescriptor.FromProperty(Button.IsPressedProperty, typeof(Button));
descriptor.AddValueChanged(this.button, new EventHandler(IsPressedChanged));
If you are using MVVM, you can use event triggers to solve your problem. This way, you can still separate your UI requirements from your application logic.
Example 1
Example 2

Working with Arrow Keys in a WPF Textbox (C#)

Is there any way to catch the Up/Down arrow keys in a WPF TextBox (System.Windows.Controls.Textbox) and allow them to alter the text? I've read about overiding the ProcessCmdKey method for a Windows Forms TextBox (System.Windows.Forms.TextBox), and it worked fine, but that TextBox is not nearly as flexible as the WPF one. Is there a similar method to accomplish this without having to use the old Windows Forms TextBox?
For my example, I have a TextBox that has a numeric text-mask. I want to be able increase/decrease the numeric value by using the up and down arrow keys.
You could add event handlers to KeyUp and/or KeyDown, if that doesn't get what you need, using PreviewKeyUp and/or PreviewKeyDown should.

Invoke KeyDown on WPF Control?

I have two entirely uncoupled WPF windows, however, if something particular is happening in window 1, I do want to invoke or 'simulate' a Key.Up/.Down on a control on the 2nd window. Is that possible?
Are they sharing an application? If they do, you should be able to call BeginInvoke on the control's event handler
But why to simulate a key press when you can just call a function?

Resources