2

Some of my end users have touch screens, and others have PCs. On touch screens, PreviewMouseUp/Down fire along with the touch event handlers, causing duplicate behavior (functions written in PreviewMousUp/Down get executed twice).

So my sample Button XAML:

<Button x:Name="Whatever" Background="Transparent"  MouseUp="Whatever_MouseUp" MouseDown="Whatever_MouseDown" TouchUp="Whatever_TouchUp" TouchDown="Whatever_TouchDown">
    <StackPanel>
        <TextBlock x:Name="WhateverText" Text="Soemthing" FontSize="13"/>
        <Image x:Name="WhateverImage" Source="bla/bla/bla"/>
    </StackPanel>
</Button>

Why MouseDown and MouseUp event handlers are not getting fired on the PC?

If I execute on a touch screen, it works like a charm (Touch event handlers). However, on my PC(VS-2015) it doesn't work at all. Please and thanks

Khalil Khalaf
  • 9,259
  • 11
  • 62
  • 104

2 Answers2

3

It seems that the Click event handler prevents firing events of MouseDown and MouseUp. I expect that because when I build custom controls like buttons from scratch, I use these events to fire Click event. This is my expectation only.

Anyway, I tried it and PreviewMouseDown/Up is fired on both touch and non-touch if you didn't implemented TouchDown/Up. But if you implemented TouchDown/Up with them, execution on TOUCH will be like this: > TouchDown > PreviewMouseDown > TouchUp > PreviewMouseUp. And execution on NON-TOUCH will be like this: > PreviewMouseDown > PreviewMouseUp. So I suggest on you to use PreviewMouseDown/Up because it works for both touch and non-touch.

Also you can use MouseRightButtonDown/Up it will work with you. But you will note that MouseDown/Up is fired after it. You can simply prevent that by adding e.Handled = true; inside MouseRightButtonDown/Up handlers.

Try to make use of these hints and if you couldn't solve it just tell me and I'll think with you. Good luck.

Ahmed Abdelkarim
  • 277
  • 2
  • 10
  • "_PreviewMouseDown/Up is fired on touch and non-touch. But PreviewMouseDown is not fired with touch_" I didn't understand this could you please explain? And I want the `MouseLeftButton` which I tried and failed already – Khalil Khalaf May 17 '16 at 22:54
  • Sorry for that, I edited the answer. Try to concentrate and test it yourself and I'm sure you will solve the issue. – Ahmed Abdelkarim May 18 '16 at 09:50
  • `PreviewMouseDown` / `Up` pair alone worked on both Touch and Non Touch devices which almost answers this question. However, this caused me another problem in this question: http://stackoverflow.com/questions/37301113/why-a-task-is-happening-on-touch-up-while-i-coded-it-in-touch-down and **Case 4** in this question: http://stackoverflow.com/questions/37283340/how-can-i-combine-touch-and-mouse-event-handlers-without-producing-duplicate-beh – Khalil Khalaf May 18 '16 at 13:35
2

I am unsure as to why you are not simply using the Click event which should work for both mouse and touch, but the reason you are not seeing MouseUp and MouseDown is explained here: WPF MouseLeftButtonUp Not Firing. Unless you create your own button control and control the container itself or use your own Button Template, PreviewXXX events are your best bet if Click Event is not an option. Even if you set event handlers on a StackPanel in a template, you do not get MouseUp events because the Button will have the mouse captured due to the default behavior of the Button control. The code below will get MouseDown event for StackPanel only but MouseUp will never get fired. Just for verification I added Click event and it was fired every time I released the mouse within the Button, which is expected behavior for Button.

    <Button Click="Whatever_Click" x:Name="Whatever" MouseUp="Whatever_MouseUp" MouseDown="Whatever_MouseDown" TouchUp="Whatever_TouchUp" TouchDown="Whatever_TouchDown">
        <Button.Template>
            <ControlTemplate TargetType="{x:Type Button}">
                <StackPanel x:Name="stackButton" MouseUp="Whatever_MouseUp" MouseDown="Whatever_MouseDown" TouchUp="Whatever_TouchUp" TouchDown="Whatever_TouchDown">
                    <TextBlock x:Name="WhateverText" Text="StackPanelClick" FontSize="13"/>
                    <Image x:Name="WhateverImage" Source="dice.png"/>
                </StackPanel>
            </ControlTemplate>
        </Button.Template>
    </Button>
Community
  • 1
  • 1
David Glass
  • 61
  • 1
  • 5
  • Hi David I am not using `Click` because all "`Up`" events don't/didn't fire with it. So if I click, the code in "Up" event doesn't execute. Could you explain this more please: "_I added Click event and it was fired every time MouseUp occurred within the Button_" – Khalil Khalaf May 18 '16 at 13:34
  • 1
    MouseUp was meant as the physical mouse up, not the event. I should have worded it as "I added Click event and it was fired every time I released the mouse within the Button", which is expected Button behavior. You will never get the "MouseUp" events because the Button control has captured the mouse and sends the Click event as a result of MouseUp event, only IF mouse is still over the Button. It has captured the mouse and handling all mouse events from there. Hopefully this link helps as well: http://stackoverflow.com/questions/942357/what-does-it-mean-to-capture-the-mouse-in-wpf – David Glass May 18 '16 at 16:08