我有一个代码,可以通过鼠标右键调用上下文菜单。
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);
}
但菜单显示在我的列表视图的中心。不在我点击鼠标的地方。如何解决?
答案 0 :(得分:2)
如果您希望在鼠标点击时显示弹出窗口,则可以使用ShowAt(UIElement,Point)
而不是ShowAt(FrameworkElement)
。
可以在点击点显示弹出窗口的代码。
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));
}