2011-08-19 59 views
0

我將一個左擊手勢綁定到一個WPF按鈕,期望它只在點擊鼠標時觸發(MouseDown + MouseUp)。但是,按下鼠標按鈕後(不釋放)它會立即啓動。LeftClick鼠標向下發射?

  1. 這是綁定到一個左鍵點擊正確的方法是什麼?
  2. 如何區分點擊和按下事件處理程序?

示例代碼:

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"); 
    } 
} 

回答

2

我不會用這些綁定,所有我知道你不能從他們那裏得到一個正確的點擊(嚴重的是,是誰設計的?)。從我所看到的,很難得到actual click on an arbitrary control。我建議你圍繞任何你想要點擊的按鈕,並使用Button的Command/Click -Event。

更改按鈕的模板,這使其不可見:

<ControlTemplate TargetType="{x:Type Button}"> 
    <ContentPresenter/> 
</ControlTemplate> 
+0

我使用的命令綁定,因爲我需要動態地在運行時創建的一些按鈕,而且我認爲這是做這樣的推薦方式WPF中的東西。在運行時創建按鈕時,我猜你在暗示我編程分配了Click事件? – Travis

+0

@Travis:你也可以設置命令,如果你想,取決於你的用例。 –

+0

有什麼區別? – Travis