wpf PointerPressed 在左键单击时不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14767020/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PointerPressed not working on left click
提问by xkillah
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), but it happens in case with right-click or tap. So what's wrong with that event? for example
在 WPF+C# 上为 Windows 8 创建 Metro(Microsoft UI)应用程序,我在按钮上遇到 PointerPressed 事件的困难。当我执行左键单击(通过鼠标)时不会发生事件,但在右键单击或点击的情况下会发生。那么这个事件有什么问题呢?例如
<Button x:Name="Somebutton" Width="100" Height="100"
PointerPressed="Somebutton_PointerPressed"/>
回答by xkillah
The solution is pretty simple: these events have to be handled not through XAML but thorugh AddHandler method.
解决方案非常简单:必须不通过 XAML 而是通过 AddHandler 方法处理这些事件。
SomeButton.AddHandler(PointerPressedEvent,
new PointerEventHandler(SomeButton_PointerPressed), true);
回答by Pete Baughman
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.
我遇到了这个问题,但无法使用已接受的答案,因为我的按钮是由 ItemsControl 动态创建的,并且没有可以从中调用 AddHandler 的好地方。
Instead, I sub-classed Windows.UI.Xaml.Controls.Button:
相反,我对 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
现在,消费控件可以绑定到 PointerPressPreview 而不是 PointerPressed
<local:PressAndHoldButton
x:Name="Somebutton"
Width="100"
Height="100"
PointerPressPreview="Somebutton_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.
如果需要,您可以在重写的 OnPointerPressed 方法中填充一些额外的逻辑,以便它仅在左键单击或右键单击时触发事件。任何你想要的。
回答by Shimmy Weitzhandler
FWIW, I've been facing the same issue and solved it by adding the event handler to some other control (but a button).
FWIW,我一直面临着同样的问题,并通过将事件处理程序添加到其他一些控件(但一个按钮)来解决它。
In my case I had a Buttonwrapped around a SymbolIconlike so:
在我的情况下,我有Button一个SymbolIcon像这样的包裹:
<Button PointerPressed="OnTempoPressed" PointerReleased="OnTempoReleased">
<SymbolIcon Symbol="Add" />
</Button>
What I did I just removed the Buttonwrap and replaced it with a ViewBox, then added the handlers to the ViewBoxitself, everything works now:
我所做的我只是删除了Button包装并用 a 替换它ViewBox,然后将处理程序添加到它ViewBox本身,现在一切正常:
<Viewbox PointerPressed="OnTempoPressed" PointerReleased="OnTempoReleased">
<SymbolIcon Symbol="Add"/>
</Viewbox>
Note that you lose the Buttonvisuals and effects (i.e. hover etc.), but to me that wasn't an issue. I think you can re-apply them from the stock styles.
请注意,您会丢失Button视觉效果和效果(即悬停等),但对我来说这不是问题。我认为您可以从库存样式中重新应用它们。
回答by Somnath
If you are you are working with a Button control then try to attach event with "Click" event.
如果您正在使用 Button 控件,请尝试使用“Click”事件附加事件。
Please note that Button control internally consider and handles PointerPressed, MouseLeftButtonDown, MouseLeftButtonUp and raise Click event. Generally Button control wont allow PointerPressed, MouseLeftButtonDown, MouseLeftButtonUp event to bubble up and fire.
请注意 Button 控件在内部考虑并处理 PointerPressed、MouseLeftButtonDown、MouseLeftButtonUp 并引发 Click 事件。通常 Button 控件不允许 PointerPressed、MouseLeftButtonDown、MouseLeftButtonUp 事件冒泡并触发。

