简体   繁体   English

如何在背后调用Button上的MouseDoubleClick

[英]How to invoke MouseDoubleClick on Button code behind

How can I invoke MouseDoubleClick event on Button code behind? 如何在后面的Button代码上调用MouseDoubleClick事件?

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 : 您需要使用MouseButtonEventArgs代替简单的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: 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. PS:我不确定应该为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. 0在此演示中效果很好,但是我不知道它是否可以与更复杂的交互一起使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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