如何在后面的Button代码上调用MouseDoubleClick事件?
我尝试过这样的事情:
btn.RaiseEvent(new RoutedEventArgs(Control.MouseDoubleClickEvent));
但是当我这样做时,我收到以下错误:
Object of type 'System.Windows.RoutedEventArgs' cannot be converted to type 'System.Windows.Input.MouseButtonEventArgs'.
答案 0 :(得分:1)
您需要使用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>
P.S。:我不确定将MouseButtonEventArgs
- The time when the input occurred.
的第二个构造函数参数的值给出什么。 0在这个演示中运行良好,但它是否适用于我不知道的更复杂的交互。