Programowo otwarte menu kontekstowe przy użyciu automatyzacji interfejsu użytkownika?

Usiłuję zaimplementować menu kontekstowe prawym przyciskiem myszy za pomocą automatyzacji interfejsu użytkownika. Ponieważ automatyzacja interfejsu użytkownika nie ma macierzystego wzorca prawego kliknięcia, dodam dostawcę ExpandCollapse do klasy AutomationPeer listyview i odwzorowuję rozwijanie i zwijanie na otwieranie i zamykanie menu kontekstowego.

Moje pytanie: czy istnieje lepsza metoda wywoływania menu kontekstowego, która nie wymaga próby utworzenia instancji klasy z prywatnym konstruktorem? Nie mogę używać SendKeys z Shift-F10. Chciałbym użyć usługi PopupControlService, ale jest ona oznaczona jako wewnętrzna.

Moje okropne obejście:

public class MyListViewAutomationPeer : ListViewAutomationPeer, IExpandCollapseProvider
{

    public MyListViewAutomationPeer(MyListView owner)
        : base(owner){}

    public override object GetPattern(PatternInterface patternInterface)
    {
        if (patternInterface == PatternInterface.ExpandCollapse)
        {
            return this;
        }
        return base.GetPattern(patternInterface);
    }

    public void Expand()
    {
        MyListView owner = (MyListView)Owner;

        //**********************
        //Ouch!!! What a hack
        //**********************

        //ContextMenuEventArgs is a sealed class, with private constructors
        //Instantiate it anyway ...
        ContextMenuEventArgs cmea = (ContextMenuEventArgs)FormatterServices.GetUninitializedObject(typeof(ContextMenuEventArgs));
        cmea.RoutedEvent = MyListView.ContextMenuOpeningEvent;
        cmea.Source = owner;

        //This will fire any developer code that is bound to the OpenContextMenuEvent
        owner.RaiseEvent(cmea);

        //The context menu didn't open because this is a hack, so force it open
        owner.ContextMenu.Placement = PlacementMode.Center;
        owner.ContextMenu.PlacementTarget = (UIElement)owner;
        owner.ContextMenu.IsOpen = true;

    }

questionAnswers(1)

yourAnswerToTheQuestion