Why does the KeyDown event bubble up from a TextBox? - wpf

If I type a letter into a TextBox, and its content changes according to my keypress, why does the KeyDown event continue bubbling up? I would have thought this would be 'handled' at this stage.

Since KeyDown event is a bubbling event, that's why its bubbled to its parent in your case Window. If you don't want that to bubbled to your window, you need to mark it as handled in your textBox handler itself like this -
private void TextBox_KeyDown(object sender, KeyEventArgs e)
{
e.Handled = true;
}
Whereas, if you try to hook the event PreviewKeyDown in your textBox, you will see that - Window's PreviewKeyDownEvent gets called first and later that of your textBox. Reason behind that is, it's a tunelling event. For routing strategies, refer to this link - Routing Strategies
EDIT
Morevoer, if you want to check if the KeyDown event comes from textBox, you can check the OriginalSource of your eventArgs -
private void Window_KeyDown(object sender, KeyEventArgs e)
{
// Check to make sure event comes from window and not from textbox.
if(e.OriginalSource is Window)
{
}
}

Related

PreviewlostkeyboardFocus event fires twice when I click on combobox WPF

I have wired PreviewLostKeyboardFocus event to TextBox. I handled the event. When I click on the ComboBox control, it fires twice.
If I not handled it fires only one time.
private void TextBox_PreviewLostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
e.Handled = true;
}
Can someone please help resolve this issue ?
When you set:
e.Handled = True;
You are effectively preventing the focus leaving the TextBox.
So if focus is in this TextBox and you click on another field (e.g. ComboBox) it will cause the event to fire, but the cursor will remain forever in the TextBox.
Remove this or make it conditional.

WPF: UserControl.IsFocused will not set

I created a WPF UserControl, that handles all GotFocus/LostFocus events of its child controls. I call the OnGotFocus/OnLostFocus of the UserControl, but the IsFocused property of the UserControl will not set:
void MyUserControl_Initialized(object sender, EventArgs e)
{
foreach (UIElement control in (Content as Panel).Children)
{
control.LostFocus += control_LostFocus;
control.GotFocus += control_GotFocus;
}
}
void control_GotFocus(object sender, RoutedEventArgs e)
{
if (!IsFocused)
{
e.Handled = false;
OnGotFocus(e);
}
}
void control_LostFocus(object sender, RoutedEventArgs e)
{
bool hasAnythingTheFocus = false;
foreach (UIElement control in (Content as Panel).Children)
{
if (control.IsFocused)
{
hasAnythingTheFocus = true;
}
}
if (!hasAnythingTheFocus)
{
OnLostFocus(e);
}
}
How can I set it?
Instead of the IsFocused you can use IsKeyboardFocusWithin
use the event UIElement.IsKeyboardFocusWithinChanged and it should worked perfectly.
The GotFocus method will be called when the relevant control receives logical focus... from the UIElement.GotFocus Event page on MSDN:
Logical focus differs from keyboard focus if focus is deliberately forced by using a method call but the previous keyboard focus exists in a different scope. In this scenario, keyboard focus remains where it is and the element where a Focus method is called still gets logical focus.
A more precise interpretation of this event is that it is raised when the value of the IsFocused property of an element in the route is changed from false to true.
Because this event uses bubbling routing, the element that receives focus might be a child element instead of the element where the event handler is actually attached. Check the Source in the event data to determine the actual element that gained focus.
it will get focus when the user clicks on the relevant control in the UI and/or when you call control.Focus() in your code. The IsFocused is readonly and cannot be set.

How to handle child event in parent control

In my main window, I have a child control(user control) which contains a text box . How can I handle the textchange event of the text box of child control in main(parent) window.
Please provide me some example with code as I am new to routing of events.
You should just be able to hook the event from the parent control. But since your parent control doesn't have a TextChanged event of its own, you'll need to use attached-property syntax:
<Window ...
TextBox.TextChanged="ChildTextBoxChanged">
and in your codebehind:
private void ChildTextBoxChanged(object sender, TextChangedEventArgs args)
{
...
}
You don't have to put the TextBox.TextChanged= on the Window specifically -- just any control that's a parent of the TextBox, i.e., any control that's a parent of your UserControl. The event will bubble up to each parent in turn, all the way up to the top-level Window, and can get handled anywhere along the way.
(Note that if anyone hooks the event and sets e.Handled = true, the event won't bubble past that point. Useful to know if you have handlers at multiple levels.)
this also helped me. I will have a event in the container of the child control and define the event in the code behind file. The event will handle all the text changed events for all the children.
<StackPanel TextBoxBase.TextChanged="test_TextChanged" Name="test">
<userControl/>
</StackPanel>
Create a event in your childcontrol -
public event TextChangedEventHandler TextChanged;
now add a handler for TextChanged event of TextBox in childcontrol -
private void TextBox_TextChanged(object sender, TextChangedEventArgs args)
{
if (TextChanged != null)
{
TextChanged.Invoke(this, args);
}
}
also update XAML for this handler -
<TextBox ... TextChanged="TextBox_TextChanged" ... />
Now, you have created a event in your childcontrol that fires when the Textbox's textchanged fires.
Now you only to add a handler for this event in mainwindow -
private void ChildControl_TextChanged(object sender, TextChangedEventArgs args)
{
//TODO: Add your further code here.
}

PreviewMouseDoubleClic handled ignored

I have a window containing a textBox.
On both the window AND the textBox, I add a PreviewMouseDoubleClicHandler.
Handler in the window:
private void PreviewMouseDoubleClickHandler(object sender, MouseButtonEventArgs e)
{
Debug.WriteLine("handler in the window");
e.Handled = true;
}
handler in the textBox:
private void PreviewMouseDoubleClickHandler(object sender, MouseButtonEventArgs e)
{
Debug.WriteLine("handler in the textBox");
e.Handled = true;
}
now, when I double-click on the textBox, I expect to go first into the window's Handler, print the debug line, then handle the event, then nothing more. I thought the textBox's handler would not fire since the event has already been handled by the window.
This does not work like this though: I get both handlers fired.
The weird thing is: It works fine with the PreviewMouseDown event. If I do exactly the same thing but with PreviewMouseDownEvents, I get the behavior I expect, i.e.: the window handles the mouseDown and the textBox's handler is not fired.
so Why does this not work with the doubleClick event? Am I doing something wrong? Is it supposed to work like this? is the doubleClick Event managed in a different way that prevents me from using the advantages of tunneling?
The behavior is by design, please see: http://msdn.microsoft.com/en-us/library/system.windows.controls.control.previewmousedoubleclick.aspx

Loss of Silverlight mouse up events after mouse capture?

I created a very simple test control that has a Rectangle on a canvas (within other containers, but inconsequential). The Rectangle has event handlers for mouse down, mouse move, and mouse up. If I capture the mouse in the Rectangle's MouseLeftButtonDown event, I do not receive a corresponding MouseLeftButtonUp event.
Some code:
private void rect1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (_captured = CaptureMouse())
{
_offset = new Point(Canvas.GetLeft(rect1), Canvas.GetTop(rect1));
_origin = e.GetPosition(RootCanvas);
e.Handled = true;
}
}
private void rect1_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
if (_captured)
{
ReleaseMouseCapture();
_captured = false;
e.Handled = true;
}
}
I attached event handlers for the container elements as well, just to make sure one of them wasn't getting the mouse-up event somehow, but none of them were. Is there an expectation of this in Silverlight that I haven't yet learned?
I think you are little confused about what is actually capturing the mouse events.
Consider when you do this:-
if (_captured = CaptureMouse())
what object is the CaptureMouse actually being called against?
Answer: The user control for which your code is the code-behind. Had you wanted the rectangle to capture the mouse you would do:-
if (_captured = rect1.CaptureMouse())
CaptureMouse(); from mouseDown Event and then try.

Resources