PreviewMouseDoubleClic handled ignored - wpf

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

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.

Silverlight SDK DataGrid not always fire MouseLeftButtonUp event?

Why does the DataGrid not always fire the MouseLeftButtonUp event?
I try to implement a single click behaviour on DataGridTextColumn and bind to this event.
dataGrid.MouseLeftButtonUp += OnDataGridMouseLeftButtonUp;
In the handler I call BeginEdit() and set focus on the TextBox element. It works when I get the event, but it is not always fired? Does anyone know how to fix that?
Thanks!
Try using AddHandler instead.
dataGrid.AddHandler(UIElement.MouseLeftButtonUpEvent,
new MouseButtonEventHandler(OnMouseLeftButtonUp), true)
...
private void OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
// etc...
}

Why does the KeyDown event bubble up from a TextBox?

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)
{
}
}

this.dragmove() interfering with mouseup event wpf

I have a UserControl in WPF.
The UserControl has a MouseLeftMouseButtonUp event.
The problem is- the Window has a this.DragMove() method in its MouseDown event which seems to interfere with the MouseLeftMouseButtonUp in the User Control (I need the this.DragMove() method to move the borderless window).
Any ideas?
Thanks!
DragMove is a synchronous call; it blocks until the user is done moving the window. This means once DragMove returns, the left button is up. Add your code immediately after your DragMove() call and you should be fine.
I ran into the same problem but found the answer. Your Window_MouseDown event will not run if the MouseDown has already been handled, so all you have to do to prevent DragMove from interfering with your control's mouse events is add this code:
Private Sub YourControl_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles YourControl.MouseDown
e.Handled = True
End Sub
private void MouseDown(object sender, MouseButtonEventArgs e)
{
e.Handled = true;
if (e.LeftButton == MouseButtonState.Pressed)
{
this.DragMove();
}
if (e.LeftButton == MouseButtonState.Released)
{
//your mouse up code
}
}
I had issues like this too and found that using click events instead of mousebutton events kept from firing dragmove. For my fix I just had to swap a label for a button. It's possible you could subclass a different element for your usercontrol so that you could pick up a click event to use?

Silverlight: MouseLeftButtonDown timer to repeatedly fire an event

I am trying to create a custom scrollbar and am using images as button.
For now a simple
I can handle the MouseLeftButtonDown and Up event just fine but what I'd like to do is while its held down, every so many millisecond trigger an event is fired.
I tried something like this but it isn't quite working. Suggestions?
public delegate void Changed(RangeScrollButtonControl sender, int value);
public event Changed OnChanged;
private System.Threading.Timer Timer;
private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
this.Timer = new System.Threading.Timer(Timer_Callback, null, 0, 100);
}
private void Image_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
this.Timer = null;
}
private void Timer_Callback(object sender)
{
if (this.OnChanged != null)
{
this.OnChanged(this, 1);
}
}
The piece of functionality you're looking for is a RepeatButton, this will fire it's Click event repeatedly while the mouse button is held down. You can configure the delay and the interval of the events.
You could then style this button to use the image at Silverlight Show
Hope this helps.
Which piece "isn't quite working" ?
Also, could you restyle or retemplate Silverlght's scrollbar similar to what is seen in this blog post to get what you need?
I would use a Storyboard as a timer. Something like:
Then you can do a MouseSTB.Begin. Once the Storyboard is finished you can catch it in the MouseSTB.Completeed Event. In that event you can do what ever you need to do and then just start it over again. It can easilly be controled by setting some flags on the mouseover, mouseenter and mouseleave events. I use these timers in a lot of place and they work just fine and they do not peg the processor.

Resources