WPF UserControl OnGotFocus/OnLostFocus - wpf

I've created a WPF UserControl with some TextBoxes, but if I click into a TextBox on it, the OnGotFocus of the UserControl is not called.
How can I get it? Or what is the best practice in that situation?

The OnGotFocus method is fired in response to the GotFocus routed event that have the bubbling routing strategy. This method is not fired because the corresponding event is processed at the embedded child control level.
You can check GotFocus/LostFocus events. These event should be fired for your TextBox and UserControl as well.

Related

Attached Command Behavior and LostFocus

I am using the method described here to attach a ViewModel ICommand to the LostFocus event of a Combobox, by setting CommandBehavior.RoutedEventName="LostFocus". I expected the event to fire at the same time the binding for UpdateSourceTrigger=LostFocus fired, but this turns out not to be the case.
The selecteditem Binding UpdateSourceTrigger=LostFocus fires whenever the keyboard tabs away, or after the user actually selects an item from the dropdown by clicking (not sure why this causes lostfocus, but at least it fires AFTER a selection is made).
The attached behavior event fires anytime the user clicks on the Combobox. Immediately. If using the keyboard it behaves normally, firing when you tab away from it. However, when using the mouse, the event fires when the control GAINS focus, before the user has even made a selection. Is there any way to make this behave like lostfocus does for the selecteditem?
Edit: I am curious if another answer exists, but I found a way around this problem, by setting up an additional binding. SelectedItem updates by defualt, handling the normal property change notifications, and selectedvalue updates on lostfocus, handling only the command I was trying to run. Binding looks like this:
SelectedItem="{Binding Path=SelectedCustomer, Mode=TwoWay}"
SelectedValuePath="CM_CUSTOMER_ID"
SelectedValue="{Binding Path=CustomerLostFocus, UpdateSourceTrigger=LostFocus}"
You would need to check the OriginalSource of the event arguments for the LostFocus event:
The LostFocus event is a bubbling event. This means that if multiple
LostFocus event handlers are registered for a sequence of objects
connected by parent-child relationships in the object tree, the event
is received by each object in that relationship. The bubbling metaphor
indicates that the event starts at the object that directly receives
the input condition, and works its way up the object tree. For a
bubbling event, the sender available to the event handler identifies
the object where the event is handled, not necessarily the object that
actually received the input condition that initiated the event. To get
the object that initiated the event, use the OriginalSource value of
the event's RoutedEventArgs event data.
So for the ComboBox, you may receive events for the various focusable elements inside the ComboBox.

Using triggers to fire the Items.CurrentChanging event on a tab control

I am using MVVM for a project and my question relates to using triggers.
The Items property of the TabControl has a property called items and items has an event called CurrentChanging. (TabControl.Items.CurrentChanging)
How do I wire up an event on the child of my main object using triggers?
Thanks
Edit:
This is using WPF and the MVVM-Light toolkit
This was solved by creating a Routed Event that was raised when the CurrentChangingEvent was fired in the code behind.
To make it fire I needed to add in the IsSynchronizedWithCurrentItem = true;

wpf: TextChanged event fired on setting DataContext

I've got a simple View with a single textbox that gets databound to a simple ViewModel with a single string property.
I need to catch the TextChanged event of that textbox so that I can do a little validation magic.
The problem that I am running into is that the TextChanged event fires for that textbox when the DataContext is set for the View.
Is there a standard mechanism that I can use to determine if the event is firing because of the DataContext being set versus when the user is making changes?
Thanks!
As far as I know there is no such mechanism. What you should do instead is to do your validation magic using standard means of WPF. Please see the following link: http://msdn.microsoft.com/en-us/library/ms752347.aspx#data_validation.
Anyway, as long as you use MVVM you can always detect that text has changed in the setter of the bound property in your view model.

WPF - Handling a RoutedEvent in the parent for a specific control instance

In my user control I have several textboxes e.g. Textbox1, Textbox2.
In the parent I only want to handle KeyDown events raised from Textbox1, not all Textbox
I have added a handler in the parent
this.AddHandler(TextBox.KeyDownEvent, new RoutedEventHandler(OnTextboxGoToPageKeyDown));
but of course this will handle all KeyDown events which I don't want.
Do I have to add some conditional logic in the RoutedEventHandler to check where the event was raised from?
or is there a better way?
What's the best way to do this?
Thanks.
The RoutedEventArgs in your OnTextboxGoToPageKeyDown has a property named Source which have a references to the Object who raised the event. You can ask for that property when you need to execute your logic.
HTH

Tabitem PreviewMouseLeftButtonDown is raised every time when mouse is pressed to the inside content. How to avoid this?

In WPF, I have raised PreviewMouseLeftButtonDown for a TabItem. I want this event to raise when TabItem's header is clcked. The TabItem's content is a TextBox and a Button, but whenever I click on the TextBox or Button, TabItem's PreviewMouseLeftButtonDown is raised. How can it be avoided?
Please help,
Thanks
This is due to tunneling in Wpf, you can stop tunneling by handling this event at root and in the handler write:
e.Handled = true;
then it will not tunnel down.
And then if you want to handle it for your textbox or button
use AddHandler method to assign handler to the event instead of using normal += format.
button.AddHandler(Button.ClickEvent, new RoutedEventHandler(OnbuttonClick));
Check this for details:
http://msdn.microsoft.com/en-us/library/ms742806.aspx#event_handing

Resources