显示上下文菜单

发布于 2024-10-08 07:53:11 字数 512 浏览 0 评论 0原文

尽管搜索了一段时间,但我很难找到这方面的良好文档。

我希望在我的应用程序中有一个上下文菜单,它可以复制其他点击并按住上下文菜单所见的行为,例如将应用程序从应用程序列表固定到开始屏幕。

这是我的上下文菜单:

                <toolkit:ContextMenuService.ContextMenu>
                    <toolkit:ContextMenu x:Name="sectionContextMenu">
                        <toolkit:MenuItem Header="Hide this section from this list" />
                    </toolkit:ContextMenu>
                </toolkit:ContextMenuService.ContextMenu>

如何让它显示?

I'm having difficulty finding good documentation for this, despite searching for a while.

I'd like to have a context menu in my app that replicates the behavior seen with other tap-and-hold context menus, like pinning an app to the start screen from the app list.

Here is my Context menu:

                <toolkit:ContextMenuService.ContextMenu>
                    <toolkit:ContextMenu x:Name="sectionContextMenu">
                        <toolkit:MenuItem Header="Hide this section from this list" />
                    </toolkit:ContextMenu>
                </toolkit:ContextMenuService.ContextMenu>

How do I make it show?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

季末如歌 2024-10-15 07:53:11

上下文菜单需要附加到您希望用户点击并按住的元素。

<Border Margin="0,12" BorderBrush="{StaticResource PhoneForegroundBrush}" BorderThickness="2" Background="Transparent" VerticalAlignment="Center" Padding="16">
   <toolkit:ContextMenuService.ContextMenu>
      <toolkit:ContextMenu x:Name="sectionContextMenu">
         <toolkit:MenuItem Header="Hide this section from this list" />
      </toolkit:ContextMenu>
   </toolkit:ContextMenuService.ContextMenu>
   <TextBlock Text="Tap and hold here to invoke a ContextMenu" Style="{StaticResource PhoneTextNormalStyle}"/>
</Border>

用户现在可以通过点击并按住此 Border 元素的内容来调用上下文菜单。

The context menu needs to be attached to the element that you want the user to tap and hold.

<Border Margin="0,12" BorderBrush="{StaticResource PhoneForegroundBrush}" BorderThickness="2" Background="Transparent" VerticalAlignment="Center" Padding="16">
   <toolkit:ContextMenuService.ContextMenu>
      <toolkit:ContextMenu x:Name="sectionContextMenu">
         <toolkit:MenuItem Header="Hide this section from this list" />
      </toolkit:ContextMenu>
   </toolkit:ContextMenuService.ContextMenu>
   <TextBlock Text="Tap and hold here to invoke a ContextMenu" Style="{StaticResource PhoneTextNormalStyle}"/>
</Border>

The user can now invoke the context menu with a tap and hold on the content of this Border element.

Smile简单爱 2024-10-15 07:53:11

根据内容不同项目的独特上下文菜单。

private ContextMenu CreateContextMenu(ListBoxItem lbi)
{
    ContextMenu contextMenu = new ContextMenu();
    ContextMenuService.SetContextMenu(lbi, contextMenu);
    contextMenu.Padding = new Thickness(0);

    string item_1 = "item 1";
    if(lbi.Content is string) {
        item_1 = lbi.Content as string;
    }
    contextMenu.ItemsSource = new List<string> { item_1, "item 2", "item 3" };
    contextMenu.IsOpen = true;
    return contextMenu;
}

private void Results_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (Results.SelectedIndex == -1) return;
    int index = Results.SelectedIndex;

    ListBoxItem lbi = Results.ItemContainerGenerator.ContainerFromIndex(index) as ListBoxItem;

    CreateContextMenu(lbi);
    Results.SelectedIndex = -1;
}

Unique context menu for different items depending on the content.

private ContextMenu CreateContextMenu(ListBoxItem lbi)
{
    ContextMenu contextMenu = new ContextMenu();
    ContextMenuService.SetContextMenu(lbi, contextMenu);
    contextMenu.Padding = new Thickness(0);

    string item_1 = "item 1";
    if(lbi.Content is string) {
        item_1 = lbi.Content as string;
    }
    contextMenu.ItemsSource = new List<string> { item_1, "item 2", "item 3" };
    contextMenu.IsOpen = true;
    return contextMenu;
}

private void Results_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (Results.SelectedIndex == -1) return;
    int index = Results.SelectedIndex;

    ListBoxItem lbi = Results.ItemContainerGenerator.ContainerFromIndex(index) as ListBoxItem;

    CreateContextMenu(lbi);
    Results.SelectedIndex = -1;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文