Issue with WPF Focus - c#

I have code that handles the LostFocus event of my controls. It validates the value and in some cases will enable a subsequent control. For instance, there might be a ComboBox that allows a user to select a country. The subsequent ComboBox allows the user to select a state. IF the currently selected country is not the USA, the state ComboBox is disabled. If the user selects "USA" and then tabs out of the combo box, the LostFocus code enables the state ComboBox. However, the State ComboBox does not get focus, instead focus goes to the control that follows the State ComboBox.
I've tried using the PreviewLostKeyboardFocus to handle the event instead with no luck. I'm kind of at a loss as to figuring out someway to hack WPF to get this work. Any suggestions?

try validating when the data changes, not the UI. You can add validation rules that will fire when the property is updated from the binding. Then you can use a style trigger to activate the control in question.
Check this article it should help.

I'm guessing what is happening is it determines the control to tab to before the LostFocus event fires, thereby skipping the State combo box since it is disabled. Here's the information for how focus works in WPF. What you will want to do is in your handler, determine if it should be going to the State combo box next, and programatically focus that element via the FocusManager class.

Related

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?

Textbox leave event unless tab changes

Using c# winforms vs2008
I'm using a TabControl on a form. Each tab has a different set of input fields. Some of those fields has to be completed. Such as Username. Multiple users will use the same PC, so a the username must remain static. I have a leave event on the require fields which triggers when the textbox loses focus to check if a value was added. In the case of Username, a popup would then presents possible values. That works pretty awesome to ensure accuracy and speed. To problem comes in when the user clicks on another tab, the textbox leave event triggers. How can I prevent the leave_event code from running if the focus changes to a control not on the current Tab?
I have tried different event handlers, but the leave event seem to occur first. I tried textbox validating event handler, but also no success. I tried adding a If-statement in front of the code to check the tab number, or tabcontrol state - no joy - as before anything else changes, the leave event fires.
The only remaining thing I can think of is to loop through all the controls on the tab to check if they have focus, but that just feels messy.
Any other suggestions?
I have found a partial solution. Using a Mouse_Enter and _Leave event on the tab, I set a flag to determine whether the mouse was clicked in the form or outside. If the flag is set to false, the leave event dont run. This will not solve short cut key presses though. But that I can handle later.

I need to be able to type into the ComboBox without it causing a SelectionChanged event

I have a ComboBox which acts as a list of profiles. When the user clicks the drop down list, and selects a profile, it loads settings based on that profile. I want the user to be able to modify the settings, and save them as their own profile. To do that, I want them to name their profile by typing into the ComboBox. The problem is, while typing, the SelectionChanged event is triggering, causing any profiles with similar names to load. I need the user to be able to type into the combobox, without it selecting one of the items. Or, I need to make the code that is triggered on SelectionChanged event only trigger when the user has selected an item by clicking it on the drop down box.
you can define a local bool variable m_dontHandleSelection and set it to true before you update the combo in yore code behind.
that way you can check it in the SelectionChanged handler and return without doing anything,
except setting the boolean to false

Changing a ComboBox from DropDown to DropDownList I lose options

Specifically, in my original design, I had Text comments in my ComboBox at load-up time, but then realised that users could type into the box, so I changed the DropDownStyle to DropDownList. Unfortunately, whilst this prevents the user from typing into the box, it also removes the Text from my VS2010 design and also ignores my C# updates to the Text in the program.
Is this normal behaviour or do I have a problem, or do I need to do this via another parameter ?
Keep your DropDownStyle on DropDown.
now on KeyPress event add the following:
e.Handled = true;

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