我将左键单击手势绑定到WPF按钮,期望它仅在单击鼠标时触发(MouseDown + MouseUp)。但是,按下鼠标按钮(不释放)时,它似乎会立即触发。
示例代码:
public partial class WpfTest : UserControl
{
// Gesture for clicking
public static MouseGesture MouseClickGesture = new MouseGesture(MouseAction.LeftClick);
// Logon command/gesture binding
public static RoutedUICommand LogonCommand = new RoutedUICommand();
public static MouseBinding LogonClickBinding = new MouseBinding(LogonCommand, MouseClickGesture);
public WpfTest()
{
InitializeComponent();
CommandBindings.Add(new CommandBinding(LogonCommand, LogonClicked));
Logon.InputBindings.Add(LogonClickBinding);
}
private void LogonClicked(object sender, ExecutedRoutedEventArgs e)
{
MessageBox.Show("LogonClicked");
}
}
答案 0 :(得分:2)
我不会使用这些绑定,因为我知道你无法从他们那里得到适当的点击(严重的是,谁设计了这个?)。从我所看到的情况来看,获得actual click on an arbitrary control是非常困难的。我建议你将一个按钮包裹在你想要点击的任何地方,并使用按钮的Command
/ Click
- 事件。
将Button的模板更改为此模板以使其不可见:
<ControlTemplate TargetType="{x:Type Button}">
<ContentPresenter/>
</ControlTemplate>