【问题标题】:How to add context menu in Windows 10 UWP on ListView Item?如何在 Windows 10 UWP 中的 ListView 项上添加上下文菜单?
【发布时间】:2016-10-06 00:38:48
【问题描述】:

我正在使用 Windows 10 UWP 应用程序并在照片应用程序中遇到如下长按菜单

有人可以建议如何在 Windows 10 中创建这种类型的菜单吗?

我检查了 PopupMenu 控件,但它只允许菜单中有 6 个选项。我想使用 C# 而不是 XAML 创建此菜单。

【问题讨论】:

    标签: c# uwp windows-10-universal


    【解决方案1】:

    您可以为此目的使用Flyout,然后在项目的数据模板中,定义订阅事件并显示您的菜单。示例可能如下所示:

    <ListView>
        <ListView.ItemTemplate>
            <DataTemplate>
                <Grid Holding="Grid_Holding" VerticalAlignment="Stretch">
                    <FlyoutBase.AttachedFlyout>
                        <MenuFlyout>
                            <MenuFlyoutItem x:Name="EditButton" Text="Edit"/>
                            <MenuFlyoutItem x:Name="DeleteButton" Text="Delete"/>
                            <MenuFlyoutSubItem Text="OtherItems">
                                <MenuFlyoutItem Text="Inside1"/>
                                <MenuFlyoutItem Text="Inside2"/>
                                <MenuFlyoutItem Text="Inside3"/>
                            </MenuFlyoutSubItem>
                        </MenuFlyout>
                    </FlyoutBase.AttachedFlyout>
                    <TextBlock Text="{Binding}" VerticalAlignment="Center"/>
                </Grid>
            </DataTemplate>
        </ListView.ItemTemplate>
        <x:String>Item 1</x:String>
        <x:String>Item 2</x:String>
        <x:String>Item 3</x:String>
        <x:String>Item 4</x:String>
        <x:String>Item 5</x:String>
    </ListView>
    

    以及后面的代码:

    private void Grid_Holding(object sender, HoldingRoutedEventArgs e)
    {
        FrameworkElement senderElement = sender as FrameworkElement;
        // If you need the clicked element:
        // Item whichOne = senderElement.DataContext as Item;
        FlyoutBase flyoutBase = FlyoutBase.GetAttachedFlyout(senderElement);
        flyoutBase.ShowAt(senderElement);
    }
    

    因此,您应该会看到如下图所示的内容。

    您还可以在at MSDN找到更多帮助。


    在 cmets 之后编辑。

    通常,您在 XAML 中创建的所有内容都可以在后面的代码中创建。如果你想创建一个Flyout,那么它可以是这样的:

    private bool startedHolding = false;
    private void Grid_Holding(object sender, HoldingRoutedEventArgs e)
    {
        // simple checkup for holding release for this sample, though this probalby need better handling
        startedHolding = !startedHolding;
        if (startedHolding)
        {
            MenuFlyout myFlyout = new MenuFlyout();
            MenuFlyoutItem firstItem = new MenuFlyoutItem { Text = "First item" };
            MenuFlyoutItem secondItem = new MenuFlyoutItem { Text = "Second item" };
            MenuFlyoutSubItem subItem = new MenuFlyoutSubItem { Text = "Other items" };
            MenuFlyoutItem item1 = new MenuFlyoutItem { Text = "First sub item" };
            MenuFlyoutItem item2 = new MenuFlyoutItem { Text = "Second sub item" };
            subItem.Items.Add(item1);
            subItem.Items.Add(item2);
            myFlyout.Items.Add(firstItem);
            myFlyout.Items.Add(secondItem);
            myFlyout.Items.Add(subItem);
            FrameworkElement senderElement = sender as FrameworkElement;
            myFlyout.ShowAt(senderElement);
        }
    }
    

    【讨论】:

    • 感谢我们可以创建您使用 c# 而不是 Xaml 创建的弹出窗口吗?
    • @KinjanBhavsar 您在 XAML 中创建的所有内容都可以在后面的代码中创建。在 XAML 中,它更容易做到。请参阅我的编辑以获取示例。
    • 感谢它按预期工作,但我们可以更改背景颜色和前景色吗?另外,我们可以改变位置吗?
    • @KinjanBhavsar 一般来说,我认为您可以很少使用弹出式演示者样式并尝试在特定元素上显示它(尽管我没有玩过那么多)。最后,您可以将自己的内容定义到通用 Flyout 控件中。
    • @KinjanBhavsar Shurely 它不能在桌面上运行,因此您不会收到任何举行事件 - 在桌面上使用 RightTapped 事件进行右键单击。
    猜你喜欢
    • 2023-03-13
    • 2016-01-08
    • 2020-08-07
    • 2020-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-27
    • 2017-05-04
    相关资源
    最近更新 更多