Button Hang & Touch Effect (WPF)

I have a WPF desktop application with Button . When I run it on a regular PC and click the mouse cursor over the button, it turns blue (the default theme for Windows). When I move the cursor, the button turns gray again. Pretty normal behavior.

But when I run it on a Windows 8 tablet, the following happens: I touch Button , it turns blue. Then I move my finger, but the button remains blue. There is no MouseLeave event. I see a blue button until I clicked somewhere else on the screen.

Is there any solution how to prevent this? I know that I can remove the whole hover effect, but I do not want to do this if there is no other way.

+4
source share
3 answers

I was able to fix this using the following behavior that uses visual states:

 public class TouchDeviceMouseOverUIElementFixBehavior : Behavior<UIElement> { protected override void OnAttached() { AssociatedObject.StylusUp += AssociatedObject_StylusUp; } protected override void OnDetaching() { AssociatedObject.StylusUp -= AssociatedObject_StylusUp; } private void AssociatedObject_StylusUp(object sender, StylusEventArgs e) { var control = sender as FrameworkElement; if (control != null) { if (!VisualStateManager.GoToElementState(control, "Normal", true)) { VisualStateManager.GoToState(control, "Normal", true); } } } } 
0
source

You can do this by removing the default mouse option of the mouse in WPF. It worked great for me.

Here is the source I found:

0
source

Source: https://habr.com/ru/post/1491145/


All Articles