简体   繁体   中英

How to invoke MouseDoubleClick on Button code behind

How can I invoke MouseDoubleClick event on Button code behind?

I've tried something like this:

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

but when I do so I receive the following error:

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

You need to use MouseButtonEventArgs in place of simple RoutedEventArgs :

Code-behind:

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>

PS: I am not sure what shall be given as the value of the second constructor param for the MouseButtonEventArgs - The time when the input occurred. . 0 works well in this demo, but whether it will work with more complex interactions I do not know.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM