簡體   English   中英

如何在背后調用Button上的MouseDoubleClick

[英]How to invoke MouseDoubleClick on Button code behind

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

我已經嘗試過這樣的事情:

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

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

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

您需要使用MouseButtonEventArgs代替簡單的RoutedEventArgs

代碼隱藏:

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:我不確定應該為MouseButtonEventArgs的第二個構造函數參數值提供什么- The time when the input occurred. 0在此演示中效果很好,但是我不知道它是否可以與更復雜的交互一起使用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM