2016-09-30 72 views
0

我有一個代碼,通過鼠標右鍵調用上下文菜單。ListView上的RightClick菜單UWP

private void GridColections_RightTapped(object sender, RightTappedRoutedEventArgs e) 
    { 

     MenuFlyout myFlyout = new MenuFlyout(); 
     MenuFlyoutItem firstItem = new MenuFlyoutItem { Text = "OneIt" }; 
     MenuFlyoutItem secondItem = new MenuFlyoutItem { Text = "TwoIt" }; 
     myFlyout.Items.Add(firstItem); 
     myFlyout.Items.Add(secondItem); 
     FrameworkElement senderElement = sender as FrameworkElement; 
     myFlyout.ShowAt(senderElement); 
    } 

但是該菜單出現在我的listview的中心。不在我點擊鼠標的地方。如何解決它?

回答

2

如果您希望在您的鼠標點擊點處顯示Flyout,您可以使用ShowAt(UIElement,Point)而不是ShowAt(FrameworkElement)

該代碼可以顯示在您的點擊Flyout。

 private void GridColection_OnRightTapped(object sender, RightTappedRoutedEventArgs e) 
    { 
     MenuFlyout myFlyout = new MenuFlyout(); 
     MenuFlyoutItem firstItem = new MenuFlyoutItem { Text = "OneIt" }; 
     MenuFlyoutItem secondItem = new MenuFlyoutItem { Text = "TwoIt" }; 
     myFlyout.Items.Add(firstItem); 
     myFlyout.Items.Add(secondItem); 

     //if you only want to show in left or buttom 
     //myFlyout.Placement = FlyoutPlacementMode.Left; 

     FrameworkElement senderElement = sender as FrameworkElement; 

     //the code can show the flyout in your mouse click 
     myFlyout.ShowAt(sender as UIElement, e.GetPosition(sender as UIElement)); 
    } 

+0

一切工作!謝謝! – SuxoiKorm