Title pretty much explains it all really. I need to know how to automatically jump to the next element (the element that would get focus if the user presses the "Tab" key), whatever that may be, in the KeyDown event of a Silverlight TextBox. My TextBoxes are generated dynamically so I can't, for example, manually code TextBox1 to set focus on TextBox2 on keydown etc. Any help would be greatly appreciated
I don't believe there is an easy way to accomplish this. The only method I know of is to use the VisualTreeHelper and get all the elements and then loop through them by their tab stop values.
If your textboxes are generated dynamically you could use a Behavior to set their tab stop properties pretty easily which would be a much better solution. Or just set it when you create them.
Related
Need help in achieving functionality for the image that I insert.
I have 4-5 textboxes and near them some type of calculator.
All TextBoxes are bound to properties in ViewModel.
All calculator buttons use Command="{Binding AddNumberCommand}" CommandParameter="9" for passing value to a property.
Problem is that I don't know how to pick the right TextBox for inputting value.
Thanks for the advice.
There are a couple ways I could think of to do this. Note that whichever way you do it, I'm pretty sure you'll have to set Focusable to false on all your Buttons, otherwise they'll steal the focus away from the TextBoxes when clicked.
Keyboard Focus Events
You could declare a field where you track the currently focused TextBox (e.g. private TextBox focusedBox;). And for each TextBox, you could handle the GotKeyboardFocus and LostKeyboardFocus events. You could use those events to track which TextBox currently has focus, and when you want to add a number you just add it to focusedBox.
You would also have to check if focusedBox is null, in case none of your TextBoxes are currently focused.
This method would ensure that the calculator buttons only ever effect those TextBoxes you want them to.
Keyboard.FocusedElement
Alternatively, you could just use the Keyboard.FocusedElement static property. This will point to whatever element currently has keyboard focus. This coud be any focusable element in your application, or null if the keyboard focus is outside your application.
You could attempt to cast Keyboard.FocusedElement to TextBox, and if the cast succeeds, append the text.
This would require much less code, but also allows the buttons to add text to any TextBox in your application. This may or may not be a problem, and could be prevented by setting some unique property on the TextBoxes you want to allow input on and checking for it, or keeping a list of said TextBoxes.
I created a class based on UIElement and my intention is to render it myself overriding OnRender. Rendering works fine. Next I want to implement focus management and continue with other aspects of LIFE, but overriding GotFocus and calling Me.Focus() in it don't do a single thing. I places my control on a Window with one another control - TextBox, and clicking on it doesn't do a single think. Tab doesn't set focus too, and TextBox is AcceptsTab negative. I know I will have to visualize focus somehow in OnRender to actually tell when the control is focused or not, but first I need to allow it to receive focus and that's where I struggle. Could you please help me out?
P.S. I tagged this with FrameworkElement because I don't have enough reputation to create a tag UIElement and leaving tags empty seemed like a silly thing to do.
Converting my comment into an answer:
I think you'd probably be better off deriving from FrameworkElement instead.
I am trying to get a control i can click on, press any keys (except tab) and have it take in the input. I tried using this and had a problem with the arrow keys changing the focus. That question is here.
The textbox seems to work but i dont like how it blinks. As in the line that shows where you next letter will be placed. How do i fix this?
Blinking is a system wide option. Check this out.
If you create a UserControl based on the standard textbox you can use [Control::IsInputKey][1] to override default behaviour which causes the control to lose focus.
I have a WPF Textbox, that I want to check that the text value is correct before I allow it to lose keyboard/focus.
I have tried setting e.Handled in the InputBox_LostFocus & InputBox_LostKeyboardFocus events, but it doesnt seem to be achieving what I want.
Any suggestions on how I can lock focus to a Textbox?
The best way to do this is to handle the PreviewLostKeyboardFocus event which is fired while the event is tunneling down to your textbox. Set handle to true and nothing else will receive the notification (meaning focus will not be transfered away from your textbox). Hope this helps.
You can call Mouse.Capture on a UIElement. This will then give you every mouse event that hapens whether on the element or not. but its tricky to use. You can capture the mouse on your text box and register for lost capture events, when you lose capture you can recapture. you have to watch out for strange behaviors. Generally its bad practice (I think) to not allow a user to move off a field. what is better is to allow them to do whatever they want, but disable the button that they push after entering data until all fields are valid (or something similar)
Here are some links
other SO question
msdn sample code
the combo box uses mouse capture to tell if the user has clicked elsewhere in the app to close the combo box if its open if you click on another control (or outside the window)
I dont know if this technique will stop you tabbing off the element. there are two kinds of focus in a wpf app. You have logical focus and keyboard focus. Multiple elements can have logical focus at once (each within a focus scope). think for example a textbox can have logical focus while you are clicking a menu (which has logical focus as well). Keyboard focus can only be in one place at a time. You are going to make a lot of work for yourself. I would seriously consider if you are doing your interaction in the right way. You could spend days getting this interaction correct. If you stop your textbox losing focus, what happens if the user clicks the close button?
heres the msdn article on focus
I am working a WPF application, where I have maintained a Menu Bar with Input Gestures i.e keyboard Shortcuts.
For Save As menu item, I have kept Ctrl+A as per User's requirement. It works fine as far as the focus is on the main window.
Now my problem is, suppose use has navigated in some Listbox in window, and if he presses Ctrl+A, then Select All functionality takes places for the list box and Save As dialog box does not get called (as i have done the command binding for this input gesture)
Any idea how can I avoid this? and yes, I can not change my input gesture. It has to be Ctrl+A. :)
Thanks
I think you could change the command bindings on the list box object to remove the binding for the command. Look at the ListBox.CommandBindings list.
You could also turn off Focusable on the ListBox so that it never receives keyboard commands.
You could also check out the eventing model. You could probably catch the keydown as the preview events "bubble up" from the root of the logical tree and then they are passed down from the end element down. They can be marked as handled on the way up or the way back down.