handle click event outside of button

旧街凉风 提交于 2019-12-01 20:09:43

You really just need to set the Background of the Canvas, as it only gets mouse input where it has "rendered content". The background could even be transparent:

<Canvas Name="canvas" Background="Transparent"
        MouseLeftButtonDown="Canvas_MouseLeftButtonDown_1">
    ...
</Canvas>

Canvas is a UIElement. This allows the use of the PointerPressed event.

private void Target_PointerMoved(object sender, PointerRoutedEventArgs e)
{
    Windows.UI.Xaml.Input.Pointer ptr = e.Pointer;

    if (ptr.PointerDeviceType == Windows.Devices.Input.PointerDeviceType.Mouse)
    {

        Windows.UI.Input.PointerPoint ptrPt = e.GetCurrentPoint(Target);
        if (ptrPt.Properties.IsLeftButtonPressed)
        {
            //do yo thang
        }
    }
}

use this instead of Canvas_MouseLeftButtonDown_1 event:

protected override OnMouseDown(MouseButtonEventArgs e)
{
    if(e.Changed == MouseButton.Left)
      {
    // Your logic on mouse down will go here
      }
    base.OnMouseDown(e);
}

with this you can click anywhere on the canvas and get the event to fire. I hope this helps..

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!