WPF: Restore keyboard focus after re-enabling container - c#

I have a Window with a Grid with two TextBoxes in it. One of them has keyboard focus. If I disable the Grid and then re-enable it some time later, the keyboard focus is not restored to the TextBox that previously held it.
What actually happens is that the cursor is shown but not blinking...
I've tried setting the focus on the TextBox in Grid's IsEnabledChanged event, but none of the approaches I've tried work (Keyboard.SetFocus, txtBox.Focus, FocusManager methods...)
NOTE: I disable and enable the Grid in Dispatcher thread, so it's not a threading issue. Also, Keyboard.FocusRestoreMode is set to Auto.
Has anybody faced this problem? Is there a way around this? What I've managed to deduce is that when Grid's IsEnabledChanged fires, the TextBox is still disabled. But I could't find an event that would fire when Grid's content is enabled also.

Related

Mouse button press understand control that loses focus (wpf - C#)

Good afternoon
in a project I have to intercept the shift of focus from one textbox to another any control present in the window when the left mouse button is pressed.
The problem is that once the key has been pressed I detect the receiving control while I would need to know the source control in order to be able to carry out checks on the data present and, in the event of an error, refocus on the source control.
At this moment I already have an event connected with the LOSTFOCUS on the source control but when I try to change the focus (once the data error is detected) to the offending TextBox this is not minimally detected and the focus goes to the new selected control.
I have been surfing the net since yesterday but I can't find anything that solves the problem.
Does anyone have any ideas on how I can fix the problem.
Thanks and good job
Perini Luca

How to disable mouse buttons in C# WPF?

During big files loading I want to disable mouse buttons to unable user to click on UI elements and triggers events.
Edit
When I am loading big size file in my WPF Caliburn Micro application I changed IsEnabled property of Listbox to false, because I want to disable a button during this process.
Next when file is loaded I changed IsEnabled property of Listbox to true.
After that every click on disabled button raised events and I did not want that.
I don't know how to remove this events, and where there are stored, so i thought that the easier way to solve this problem is to disable mouse buttons during file loading process. But it is also not easy...
Thank You in advance!
If you only want to disable mouse buttons, the user can still use the keyboard. So you need a different technique.
You can add a hidden Gird with Opacity="0.5" to your window. When you want to prevent the user from using the window, just call visible the grid.
What about overriding SelectionChanged event on ListBox and setting it to Handled = true when loading big data?

How to stop the behavior of Scroll viewer auto scrolling to visibility in WPF

I have a scroll viewer in the grid and it has a combo box (Just sample for my problem) inside of it.
When the combo box is partially visible on the scroll viewer and when i click it auto scroll to the visibility (I put combo box just for reference - facing issue with other controls as well)
I need to stop this behavior and pretty confused about it.
Refer the following images
Thanks in advance.
Finally i found the answer,
It was very tricky.
There is an event in WPF control called
RequestBringIntoView
This will fire default when the control is loaded and all we have to do is setting
e.handled= true
Thanks for helping
set CanContentScroll="False" ....

WPF TextBox focus "sticking" after disabling and enabling

I have a TextBox in a Grid that has InputBindings such that when I hit return, a search is performed in a background thread.
The IsEnabled property of the TextBox is bound to a bool property in my ViewModel called IsSearching, which is true while the background thread is running (I use a converter to negate the value).
I type in TextBox and hit enter, starting the search and disabling the TextBox. When the background thread completes, the TextBox is enabled, however the focus is messed up:
The caret is still inside the TextBox however it does not blink, and I am unable to type. I believe the TextBox has focus, but not "KeyboardFocus".
Can anyone tell me how to resolve this?
Look at that answer:
TextBox Cursor is NOT blinking
Like you said maybe you must set the focus to the keyboard

In Winforms, why is validation not being fired after leaving TextBox and entering a DataGridView?

I'm overriding the OnValidating event in a custom Winforms text box. I'm finding that if the text box (which is bound to an object) has focus and then I give a grid focus using the mouse, the OnValidating event doesn't always get fired. When I first give the grid focus, it gets fired fine. But, if put one of the grid's cell in edit (blinking cursor), from there on out it seems to not get fired when I go back between the text box and grid using the mouse. If I change focus using the tab key, the validating always gets fired. If I give focus to a non-grid control using the mouse, the validation is always getting fired.
I tried to recreate this functionality from scratch in a simple form and I can't recreate the problem. The grid I'm using in the setup where I'm getting the problem is a custom DataGridView with custom column types. I'm wondering if the grid is the problem. But, I don't see how it could affect the text box events. Any ideas?
It probably has to do with the CausesValidation property.
A control's validation is suppressed if focus is going to a control that has CausesValidation set to false. It's just a wild guess, but I'm thinking some control inside the grid has CausesValidation = false;
This property is meant for things like "Cancel" buttons, but can cause lots of confusion.

Categories

Resources