【问题标题】:How to simulate a right click on datagrid in WPF如何在WPF中模拟右键单击数据网格
【发布时间】:2018-01-03 09:45:36
【问题描述】:

我想在 WPF 中模拟右键单击数据网格,然后弹出一个上下文菜单。 但是我发现上下文菜单是通过 MS 内部的 PopupControlService 弹出的,我无法访问。现在我有了数据网格实例。如何模拟右键单击此数据网格? 谢谢大家。

【问题讨论】:

    标签: c# wpf testing datagrid simulation


    【解决方案1】:
    DataGrid.RaiseEvent(new RoutedEventArgs(ContextMenuOpeningEvent));
    

    你试过了吗?

    或者

     DataGrid.RaiseEvent(new RoutedEventArgs(MouseRightButtonDownEvent));
    

    看这里:https://timscyclingblog.wordpress.com/2012/04/05/wpf-simulate-mouse-click-in-code-behind/

    还有这里:Raising WPF MouseLeftButtonDownEvent event

    【讨论】:

    • 首先感谢您的帮助。但是这两种方法我都试过了。两者都没有工作。对于第一个它抱怨“System.ArgumentException:'System.Windows.RoutedEventArgs'类型的对象无法转换为'System.Windows.Controls.ContextMenuEventArgs'类型。”第二个:“System.ArgumentException:'System.Windows.RoutedEventArgs'类型的对象无法转换为'System.Windows.Input.MouseButtonEventArgs'类型”
    【解决方案2】:

    要绕过这个问题,您可以手动创建上下文菜单并将其显示在后面的代码中?

    【讨论】:

    • 上下文菜单已准备就绪,我正在编写代码来自动测试我们的上下文菜单行为。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-09
    • 2014-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多