WPF TextBox focus "sticking" after disabling and enabling - c#

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

Related

WPF: Restore keyboard focus after re-enabling container

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.

Set focus of an element from its lost focus

For some reason focus a text box is need, so the focus is again set to the text box from its lost focus. This leads to stack over flow in the lost focus event.
Say,
A Textbox and a button, now the focus is in Textbox and clicking on the button. In this case, need the focus to be in the textbox, so focus method of text box is invoked from its lost focus. This leads to stack over flow exception.
Why this happens and also how do the focus is retained in text box itself.
By handling PreviewKeyboardLostFocus of textbox, i have retained the Keyboard focus but how to retain the mouse focus in this?
Check if you had handled SetFocus() Event, You might have handled something there leading to StackOverflowException.
Cheers!

what is happening when by default when I databind to windows form control.

So I was asked to fix an issue with an old windows form utility that has been around a little while (least before any of my coworkers showed up). The form has a numericUpDown control that is databound. The issue was, when you clicked the up or down arrow the values would change and save OK however, if you just typed in a number and clicked save it wouldn't save. It was like the databinding never saw the change, so coming from a WPF background I guessed that changing the following
TaskDaysToComplete.DataBindings.Add("Value", taskTemplate, "DaysToComplete");
To this
TaskDaysToComplete.DataBindings.Add("Value", taskTemplate, "DaysToComplete", false, DataSourceUpdateMode.OnPropertyChanged);
would solve my problem and it did. You can now either type in a number or use the up/down arrows on the control to set the "Value" property.
My question is this, what was happening in the first place? I am guessing the default DataSourceUpdateMode was OnValidation but when does this happen and why was it OK for when using the up/down arrows but never seemed to happen when typing things in.
Thanks!
numericUD validation
validation is done on losing focus, so when you press the up/down key the textbox loses focus - triggering the validation routine.
when editing text you can make the control lose focus by clicking another control, this will cause it to validate.
the reason that the default is set to onValidate is that on value changed will cause it to validate on each character typed, which can be problematic both for performance and for correct validation.

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.

How do I disable a button cell in a WinForms DataGrid?

I have a WinForms application with a DataGridView control and a column of DataGridViewButtonCell cells within that. When I click on one of these buttons, it starts a background task, and I'd like to disable the buttons until that task completes.
I can disable the DataGridView control, but it gives no visual indication that the buttons are disabled. I want the user to see that the buttons are disabled, and to notice that the task has finished when the buttons are enabled again.
Bonus points for a method that allows me to disable the buttons individually, so I can leave one of the buttons enabled while the task runs. (Note that I can't actually give out bonus points.)
Here's the best solution I've found so far. This MSDN article gives the source code for a cell class that adds an Enabled property.
It works reasonably well, but there are two gotchas:
You have to invalidate the grid after setting the Enabled property on any cells. It shows that in the sample code, but I missed it.
It's only a visual change, setting the Enabled property doesn't actually enable or disable the button. The user can still click on it. I could check the enabled property before executing the click event, but it also seemed to be messing up the appearance when the user clicked on it. Instead, I just disabled the entire grid. That works alright for me, but I'd prefer a method that allows me to disable some buttons without disabling the entire grid.
There's a similar sample in the DataGridView FAQ.
You could give this a try:
When you click on the cell...
Check to see if the process with the current row identifier is running from a class-level list; if so, exit the cell click event.
Store the row identifier in the class-level list of running processes.
Change the button text to "Running..." or something appropriate.
Attach a basic RunWorkerCompleted event handler to your process (explained shortly).
Call backgroundWorker.RunWorkerAsync(rowIdentifier).
In the DoWork event handler...
Set e.Result = e.Argument (or create an object that will return both the argument and your desired result)
In the RunWorkerCompleted event hanlder...
Remove the row identifier from the running processes list (e.Result is the identifier).
Change the button text from "Running..." to "Ready"

Categories

Resources