PointerPressed not working on left click

前端 未结 4 804
温柔的废话
温柔的废话 2021-01-01 16:27

Creating Metro (Microsoft UI) app for Windows 8 on WPF+C#, I met difficulty with PointerPressed event on a button. Event doesn\'t happen when i perform left-click (by mouse)

4条回答
  •  时光取名叫无心
    2021-01-01 16:39

    I have encountered this problem, but was not able to use the accepted answer because my buttons were created dynamically by an ItemsControl, and there was no good place to call AddHandler from.

    Instead, I sub-classed Windows.UI.Xaml.Controls.Button:

    public sealed class PressAndHoldButton : Button
    {
        public event EventHandler PointerPressPreview = delegate { };
    
        protected override void OnPointerPressed(PointerRoutedEventArgs e)
        {
            PointerPressPreview(this, EventArgs.Empty);
            base.OnPointerPressed(e);
        }
    }
    

    Now, the consuming control can bind to PointerPressPreview instead of PointerPressed

    
    

    If you want, you can stuff some additional logic in the overridden OnPointerPressed method so that it only fires the event on left-click, or right-click. Whatever you want.

提交回复
热议问题