以编程方式添加 WP7 ContextMenu

发布于 2024-10-09 09:09:59 字数 1734 浏览 0 评论 0原文

我正在动态加载页面上的元素(读取 XML 文件的内容)。动态内容被加载到 StackPanel 中。内容的每个元素都由一个 TextBlock 和一个其他 UI 元素组成,因此对于每一对,我创建一个新的 StackPanel,然后将其添加到父 StackPanel< /代码>。代码如下所示:

TextBlock header = new TextBlock() {
        Text = "Heading 1",
        HorizontalAlignment = HorizontalAlignment.Stretch,
        VerticalAlignment = VerticalAlignment.Top,
        Foreground = (SolidColorBrush)Application.Current.Resources["PhoneAccentBrush"],
      };
TextBox item = new TextBox() {
        HorizontalAlignment = HorizontalAlignment.Stretch,
        VerticalAlignment = VerticalAlignment.Top,
      };
StackPanel sp = new StackPanel();
sp.Children.Add( header );
sp.Children.Add( item );

parentSP.Children.Add( sp );

我想向此 StackPanel 添加一个 ContextMenusp,而不是 parentSP);根据从文件中读取的某些参数,它可能是 2 个不同的上下文菜单之一。我尝试了以下方法,但它不起作用:

    ContextMenu cm = new ContextMenu();
    RoutedEventHandler clickHandler = new RoutedEventHandler( OnContextMenuClicked );

    // Add "edit" entry
    MenuItem menuItem = new MenuItem() {
      Header = "edit",
      Tag = "edit",
    };

    menuItem.Click += clickHandler;
    cm.Items.Add( menuItem );

    // Add "delete" entry
    menuItem = new MenuItem() {
      Header = "delete",
      Tag = "delete",
    };

    menuItem.Click += clickHandler;
    cm.Items.Add( menuItem );

    parentSP.Children.Add( cm );

How do I add a context menu to the StackPanelprogrammatically?

另外,有没有更好的方法来解决这个问题?也许可以将 2 种不同类型的上下文菜单存储在 XAML 资源部分中并根据需要添加它们?我尝试通过将上下文菜单添加到父级的 StackPanel.Resource 部分来执行此操作,但收到错误消息“属性元素不能是另一个属性元素的直接子级”

提前感谢您的帮助

I'm loading elements on a page dynamically (reading the contents of an XML file). The dynamic content is loaded into a StackPanel. Each element of the content consists of a TextBlock and one other UI element, so for each pair I create a new StackPanel which is then added to the parent StackPanel. The code looks like this:

TextBlock header = new TextBlock() {
        Text = "Heading 1",
        HorizontalAlignment = HorizontalAlignment.Stretch,
        VerticalAlignment = VerticalAlignment.Top,
        Foreground = (SolidColorBrush)Application.Current.Resources["PhoneAccentBrush"],
      };
TextBox item = new TextBox() {
        HorizontalAlignment = HorizontalAlignment.Stretch,
        VerticalAlignment = VerticalAlignment.Top,
      };
StackPanel sp = new StackPanel();
sp.Children.Add( header );
sp.Children.Add( item );

parentSP.Children.Add( sp );

I want to add a ContextMenu to this StackPanel (sp, not parentSP); depending on some parameters read from the file it could be one of 2 different context menus. I tried the following but it is not working:

    ContextMenu cm = new ContextMenu();
    RoutedEventHandler clickHandler = new RoutedEventHandler( OnContextMenuClicked );

    // Add "edit" entry
    MenuItem menuItem = new MenuItem() {
      Header = "edit",
      Tag = "edit",
    };

    menuItem.Click += clickHandler;
    cm.Items.Add( menuItem );

    // Add "delete" entry
    menuItem = new MenuItem() {
      Header = "delete",
      Tag = "delete",
    };

    menuItem.Click += clickHandler;
    cm.Items.Add( menuItem );

    parentSP.Children.Add( cm );

How do I add a context menu to the StackPanel programmatically?

Also, is there a better way to solve this problem? Maybe by storing the 2 different types of context menus in a XAML resources section and adding them as needed? I tried doing this by adding the context menus to the parent's StackPanel.Resource section but got an error saying "A property element cannot be the direct child of another property element"

Thanks in advance for your help

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

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

发布评论

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

评论(1

四叶草在未来唯美盛开 2024-10-16 09:09:59
ContextMenuService.SetContextMenu(sp, cm);
ContextMenuService.SetContextMenu(sp, cm);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文