Know which textBox has focus in xaml? - c#

I have 3 textboxes that will change while it's being typed on a 4th one. I have tried the TextChanged="function" and change the content of the other three, but by chaging the .Text property, the event TextChanged is triggered again providing an undesired result. I was thinking of checking which textbox has the focus at the time, but I have no idea on how to implement that. I am experienced with Java and I'm very frustrated with all this c# and XAML. Thank you in advance.

If you search for FocusManager then you will find two kind of FocusManager class. But you have to go for
Windows.UI.Xaml.Input.FocusManager
not
System.Windows.Input.FocusManager.
It has static method GetFocusedElement() which queries the focus system to determine which object in the UI has focus.

There is a FocusManager-class, which might help you. Use the textbox' parent as scope-element.

Related

WPF - Common event fired for value changed

Ok. This seems like an incredibly basic use case but fore some reason I am having an issue finding a common solution across control types.
Heres the use case:
A form is presented to the user with any editable controls. (Text box, combo, grid etc.).
The user edits a value in the control and tabs out.
Expectation is that I can wire to an event like Lost Focus and do "foo" with the changed value.
The user then gives focus back to the control and tabs out without making an edit.
Expectation is that whatever event I am wired to I can check if the value has been changed.
Is there one common event across controls that will only fire when the user has finished editing( such as tab out or enter ) and allow me to check previous state vs. current state?
Jason, you may want to look into Binding and DependencyProperties in WPF instead of tracking events in your form. You would bind a class to your form which exposes properties to be changed. Using DependancyProperties a single event is fired called "PropertyChanged".
Unfortunately is is a broad topic, but you will really get the full benefit of the WPF programming model. Searches on "dependency properties in wpf" will give you some good examples.
I think maybe this is Focus issue. There exist two different focus types: keyboard focus and logical focus. The the control that has keyboard focus is the one that has the caret, in what the user press a key and the control process that input. The a control may have the logical focus but not having the keyboard focus. Please check this in the MSDN article "Input Overview". About the other question, maybe you could process the TabControl.SelectedItemChanged for taking the event when a tab item selection changed.
Hope this is helpful to you...
What you may be interested in is implementing INotifyPropertyChanging (not just INotifyPropertyChanged).
As noted in the answer of this question, the INotifyPropertyChangING does not prevent you from changing a value, but to detect WHEN something DOES change, and what it's new value is going to be.
Hope it helps in your solution needs.
As the previous answers suggested, you would be better off by implementing a view - viewmodel structure, with your viewmodel having implemented INotifyPropertyChanged, and thus allowing you to bind to properties that will announce their changes to the UI.
If you don't want to do this, you can eventually subscribe on your input elements to the PreviewKeyUp event, check if the Tab key has been pressed and proceed from there.

TextBox LostFocus event occurs too often

I have an application in Silverlight and WPF. The error just happens in Silverlight, but same code is used in WPF.
In my application, there is a RibbonBar, with several RibbonGroups. In each RibbonGroup there are at least one RibbonButton. One of those RibbonGroups also contains four TextBox.
Every TextBox has its own OnLostFocus-Handler. When I leave a TextBox the related Handler is raised.
Now, (1) I click into one of those TextBoxes and (2) then click a RibbonButton, OnLostFocus raises and after that the RibbonButton dropdown menu appears. Everything OK.
After that, (3) I click on another RibbonButton. Again OnLostFocus raise, although - imho - it shouldn't.
So it goes on and on. After every action the TextBox keeps(or gets back, don't know) the focus and this causes the OnLostFocus-Handler to raise.
In WPF same code does not raise OnLostFocus again. Just one time, when it really lost focus.
Does anybody know, why this behaviour is that strange in Silverlight. What is really different to WPF, maybe I just have to set a property which default value ist different to WPF.
Thanks in advance.
Try using OnPreviewLostKeyboardFocus. OnLostFocus is for logical focus which you may lose for a variety reasons.
I don't know the reason for sure but I suspect that the problem you're seeing might be because the ribbon bar is in a FocusScope. When you put focus onto something inside of a focus scope then what you end up with can seem like focus bouncing around some. I'm not certain of this answer, focus is very complicated in WPF. Submitting some code samples might help.

How to Capture keydown event of a Label?

I added the event, I click in the label and press any key, but it doest go to the method. How can I capture?
I don't think labels can receive keyboard input. It will go to the control with focus, and labels can never have focus (by default, I guess you might pull some shenanigans though), most likely your events are going to your main form.
Maybe you could disguise a text box to look like a label.
To add content beyond my other comment. Have you looked at something like input bindings for what you are trying to do?
http://msdn.microsoft.com/en-us/library/system.windows.input.inputbinding.aspx

WPF - Is there a way to cause a UI element to lose focus without changing the focus to something else?

I would like to add an IsFocused property to a custom textbox that I am working on, is there any way to do this?
Unless I'm mistaken something always has to be in focus. That being said you can Focus to a label or some non-editable/non-selectable element if you need. Or you can focus another application.
In general, however, something is always in focus.

How do I move a tooltip?

I'm using the CellToolTipTextNeeded event of a DataGridView, and the tooltip is being shown under the mouse. I can get the ToolTip object out via reflection, but I don't have any control over where it's positioned since I'm not the one calling Show(). How do I move a tooltip?
How about a custom one?
I think the short answer is: you can't, not yet anyway. I would probably think about building a custom one like 'unknown (yahoo)' suggested.
You'll have to control the tooltip yourself using the Show and Hide methods if you want any control over how and where it is positioned using the 2.0 framework unless you want to get into Window's API calls.
Unfortunately, it looks like the tooltip position is set very, very early on in its creation. The tooltip only has 3 events to handle, and we aren't very interested in Dispose in this case.
The Draw event is fired after the Popup event and it seems as if the position has already been set as the Bounds property in the PopupEventArgs is readonly.
Here's a link to one of many articles (at the time of this post) on creating a custom control:
http://andrusdevelopment.blogspot.com/2007/10/implementing-custom-tooltip-in-c.html
And the MSDN documentation on using the tooltip's Show and Hide methods is actually fairly decent:
http://msdn.microsoft.com/en-us/library/44atyhsa(VS.80,printer).aspx
Sorry, but it just doesn't look like there is a quick and easy answer to this one. Hopefully someone will prove me wrong for both are sakes.
Scott
There is a good answer at this link that uses a Tooltip object.
How to override default tooltip behavior of a C# datagridview to increase the tooltip delay for particular cells

Categories