this.dragmove() interfering with mouseup event wpf - 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?

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.

XAML Window MouseDown event beats Image event

I'm using WPF to design a borderless, movable application window.
In order to manually perform the ability to drag and drop the window, I've added an OnMouseDown event to the <Window> element, that executes a corresponding C# function (this.DragMove()).
Additionally, I need an <Image> button to allow some operation (with the OnMouseUp event this time). Note that it has to be an Image tag, and not a Button.
Unfortunately, the Image event fired only when the right mouse button is clicked, probably because the left button is held to the window event. Am I right?
When someone clicks the Image button, I want only the Image event to be triggered. How can I do it?
Problem you're facing is most probably related to event routing. The idea is that if your handler doesn't mark event as a Handled it will be routed to the next listener unless reach end of chain or one of listeners/handlers will set it as Hadnled.
So if you have MouseDown event handler for both Window and Image you should keep in mind that routing will stop at a point when you will set e.Handled = true;:
private void Window_OnMouseDown(object sender, KeyEventArgs e)
{
e.Handled = false; // will NOT break event chain;
}
You can always check a type of sender so it will make possible for you to differ Image and Window events:
private void Image_OnMouseDown(object sender, KeyEventArgs e)
{
if (sender is Image)
{
// Handle Image.MouseDown
e.Handled = true; // we don't need to push event further;
}
}
Its because of WPF bubbling and tunnelling events. so what u can do is whenever u handle event on button use bubbling for that means you can use previewevents for that for both button and window and whenever you just want to handle event for button then after last line of code in button click just write down like this.
e.handled=true;
// here e is the event argument which u will get in your preview event.so now window dragging event will not work.
i would just suggest first clear the idea of bubbling(preview mouse event) and tunneling in wpf.
Difference between Bubbling and Tunneling events
and go through some of the example of bubbling and tunnelling. you will get better idea.
http://www.codeproject.com/Articles/464926/To-bubble-or-tunnel-basic-WPF-events

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...
}

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

How do prevent a WPF text box inside a combo from reacting to right mouse clicks?

I creating a custonmized box class (inherits from ComboBox). I don't want the text box to react to right mouse clicks. I can get rid of the context menu by setting this to null in ApplyTemplate, but right mouse clicks move the cursor. I tried hooking up PreviewMouseRightButtonDown in ApplyTemplate and setting Handled to True, but the event still gets through which is strange as it seems to work for the left click.
The cursor actually moves when the mouse button is released, so you want mark the MouseRightButtonUp event as handled. You could override OnMouseRightButtonUp:
protected override void OnMouseRightButtonUp(MouseButtonEventArgs e)
{
base.OnMouseRightButtonUp(e);
e.Handled = true;
}
Or you could attach a class handler to the MouseRightButtonUp event to mark it as handled:
static MyComboBox()
{
EventManager.RegisterClassHandler(
typeof(MyComboBox),
MouseRightButtonUpEvent,
new MouseButtonEventHandler(MyComboBox_MouseRightButtonUp));
}
private static void MyComboBox_MouseRightButtonUp(
object sender, MouseButtonEventArgs e)
{
e.Handled = true;
}
That will also prevent the context menu from being created without you having to set it to null explicitly.

Resources