简体   繁体   中英

Why my Mouse Event Handlers are not working when I have Touch Event Handlers?

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

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.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM