UI オートメーションを使用して右クリック コンテキスト メニューを実装しようとしています。UI オートメーションにはネイティブの右クリック パターンがないため、ExpandCollapse プロバイダーをリストビューの AutomationPeer クラスに追加し、展開と折りたたみをコンテキスト メニューの開閉にマッピングします。
私の質問は、プライベート コンストラクターを使用してクラスをインスタンス化しようとすることを伴わない、コンテキスト メニューを呼び出すためのより良い方法はありますか? Shift-F10 で SendKeys を使用できません。PopupControlService を使用したいのですが、内部としてタグ付けされています。
私のひどい回避策:
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;
}