How can I tell which TextBox had focus last? - wpf

I have an MVVM application with various TextBox controls and a virtual keypad. (This application is to run on a touch screen system, with no keyboard). To change the value of a TextBox, the user has to touch the TextBox and then use the virtual keypad to enter a number. How can my VM know which TextBox to change when it gets the command from the keypad?

if you mean WPF use FocusManager.GetFocusedElement also look here

The ViewMoel is unconcerned with the View, and as such this should not be passed to the ViewMdel.
If I needed to track this, I would use the View's codebehind (I know, I know) or create a WPF behavior* that does this for me.
*using Attached DependencyProperties, is normally how I do this.

Related

disable all input controls in a WPF Window

Is there a way to make an entire WPF Window inert after a button click?
The window is invoked via Window.ShowDialog() and after use, the window is no longer needed for interaction but I leave it open to remind the user of the inputs in TextBox's, ListBox's, and give visual feedback via OxyPlot and so on. I leave it to the user to close the window manually.
One solution is to disable all buttons but that's tedious and it still leaves TextBox's functioning. That's not optimal because for anything to be functioning creates the wrong impression that the Window remains for anything other than looking at. It would be better for every control to be non-functioning by a single setting.
I did it by putting a name on the WPF window code behind and then setting .IsEnabled false upon the appropriate button click. All buttons, combo boxes, text boxes, and even OxyPlot became inert at that point and most parts were greyed out.
Consider creating a dedicated boolean dependency property in your code-behind or viewmodel and binding IsEnabled of every TextBox to the property.

WPF - Do controls in a window automatically set the window's focus to themselves when clicked on?

It seems you can set the focus of a focusscope with FocusManager.SetFocusedElement(Dependency object, IInputElement) but I was wondering how much of this is automated by wpf. If I click another control inside the same window, will the focus automatically be changed or do I need to manually do hit testing and then set the focused element?
If you use standard controls like Button or TextBox the Focus is set automatically. If you're inventing your own controls (and don't do so by building them with the standard controls) you might have to implement the focus yourself.
Yes. The focus is automatically set to the respective controls. Because these all taken care by the Windows operating system.

To create dual window application

first window and second window will have same control like textbox or button
once some entry happend in first window it should reflect in second window also and vice-versa and in case of event also.
you can say first is mirror of second window vice-versa so any one, so can any one tell me how should i start for this.
you can use mvvm - create 1 viewmodel where you hold your data and bind the same instance of this viewmodel to both windows/views as the datacontext, create your bindings twoway and all works like you want.
btw i really dont know what you wanna do with your app :)

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;

Trap keyboard input in a WindowsFormsHost control in WPF

I have an ActiveX control inside a WinForms user control. My WinForms app loves it!
Now, moving over to WPF, I use the user control in a WindowsFormsHost control. Works great..., but I want to treat this control as a single element so the user can neatly hit TAB over the existing WPF controls AND this user control NOT to 'go inside' it. i.e. just treat it as a single control like all the others.
I think what i need is the ability to trap the keys, and in the event handler simply move focus to the next control in the sequence, but I can't seem to trap any keyboard input. Ive tried the WPF PreviewKey.. events and the like, but once the tabbing gets to the control, it seems to stay inside it and WPF events are ignored.
I couldnt find anything on this in many WPF books and the net. Can anyone suggest a way ?
Thanks,
Jack.
Can't you create some sort of a filter by doing a preview mouse down on the panel or window (whatever is the parent of your controls), this way the panel will catch it before the user control and you should set e.handled to true, and if the user control raised the tab event, keep pushing the focus until you get another control. Preview and e.Handled=ture should solve the problem.

Resources