2015-03-31 51 views
0

如何在後面的Button代碼上調用MouseDoubleClick事件?如何在背後的Button代碼上調用MouseDoubleClick

我已經試過這樣的事情:

btn.RaiseEvent(new RoutedEventArgs(Control.MouseDoubleClickEvent)); 

但是當我這樣做,我收到以下錯誤:

Object of type 'System.Windows.RoutedEventArgs' cannot be converted to type 'System.Windows.Input.MouseButtonEventArgs'. 
+0

你嘗試傳遞'新的MouseButtonEventArgs'而不是'RoutedEventArgs'嗎? – 2015-03-31 09:22:07

回答

1

你需要的地方簡單RoutedEventArgs使用MouseButtonEventArgs

代碼隱藏:

private void button_WithDoubleClick_MouseDoubleClick(object sender, MouseButtonEventArgs e) 
{ 
    MessageBox.Show("Double click"); 
} 

private void button_RaiseDoubleClick_Click(object sender, RoutedEventArgs e) 
{ 
    var args = new MouseButtonEventArgs(Mouse.PrimaryDevice, 0, MouseButton.Left) 
    { 
     RoutedEvent = Control.MouseDoubleClickEvent 
    }; 

    this.button_WithDoubleClick.RaiseEvent(args); 
} 

的XAML:

<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition/> 
     <RowDefinition/> 
    </Grid.RowDefinitions> 
    <Button Name="button_WithDoubleClick" Content="Button with double click" MouseDoubleClick="button_WithDoubleClick_MouseDoubleClick" /> 
    <Button Grid.Row="1" Name="button_RaiseDoubleClick" Content="Button to raise double click" Click="button_RaiseDoubleClick_Click"/> 
</Grid> 

P.S:我不知道什麼應該被指定爲第二個構造函數參數有關的MouseButtonEventArgs的價值 - The time when the input occurred.。 0在這個演示中效果很好,但它是否能用於更復雜的交互,我不知道。

相關問題