如何隐藏AttachedFlyout菜单的菜单项?

时间:2017-09-08 11:22:28

标签: c# uwp uwp-xaml

我能够以编程方式使用以下代码显示右键菜单,显示/隐藏所需的菜单项很容易,因为我们可以按照自己的意愿创建MenuFlyoutItem。

private void MyGrid_RightTapped(object sender, RightTappedRoutedEventArgs e)
{
    MenuFlyout myFlyout = new MenuFlyout();
    MenuFlyoutItem mnu1 = new MenuFlyoutItem { Text = "Rename" };    
    MenuFlyoutItem mnu2 = new MenuFlyoutItem { Text = "Delete" };    
    myFlyout.Items.Add(mnu1);   
    myFlyout.Items.Add(mnu2);
    mnu1.Click += mnu1_Click;
    mnu2.Click += mnu2_Click;

    FrameworkElement senderElement = sender as FrameworkElement;
    myFlyout.ShowAt(sender as UIElement, e.GetPosition(sender as UIElement));
}

但是,当我使用xaml创建菜单作为AttachedFlyout时,我无法隐藏特定的菜单项。这是我的代码:

    <ListView.ItemTemplate>
    <DataTemplate >
..............
        <Grid Name="GrdForFolderMenu">
            <FlyoutBase.AttachedFlyout >
                <MenuFlyout Placement="Right">
                    <MenuFlyoutItem Name="MenuFolderCreate" Text="New Folder" Click="MenuFolderCreate_Click" DataContext="Folders"/>
                    <MenuFlyoutItem Name="MenuFolderRename"  Text="Rename" Click="MenuFolderRename_Click"/>
                    <MenuFlyoutItem Name="MenuFolderExport"  Text="Export" Click="MenuFolderExport_Click" />
                    <MenuFlyoutSeparator />
                    <MenuFlyoutItem Name="MenuFolderDelete"  Text="Delete" Click="MenuFolderDelete_Click" />
                </MenuFlyout>
            </FlyoutBase.AttachedFlyout>
        ......
        ........

这是我的右击事件:

private void myListView_RightTapped(object sender, RightTappedRoutedEventArgs e)
{            
  FlyoutBase.ShowAttachedFlyout((FrameworkElement)sender);
}

在代码中挣扎了很长时间来隐藏特定的菜单项!请帮忙!

1 个答案:

答案 0 :(得分:4)

您只需获取MenuFlyout,然后将特定Visibility的{​​{1}}设置为MenuFlyoutItem,或将其从列表中删除。

Collapsed

我还会将if (FlyoutBase.GetAttachedFlyout((FrameworkElement)sender) is MenuFlyout menuFlyout) { // Hide 'MenuFolderExport' MenuFlyoutItem //menuFlyout.Items[2].Visibility = Visibility.Collapsed; // Or, remove 'MenuFolderExport' MenuFlyoutItem menuFlyout.Items.Remove(menuFlyout.Items[2]); } FlyoutBase.ShowAttachedFlyout((FrameworkElement)sender); 附加到您的RightTapped,并将所有GrdForFolderMenu替换为Name="xxx"