如果在多个UserControl中使用ContextMenu,则不会显示

时间:2014-04-08 18:36:13

标签: c# windows-phone-8 contextmenu

我有以下代码

    ContextMenu cm1 = new ContextMenu();
    ContextMenu cm2 = new ContextMenu();
    // Constructor
    public MainPage()
    {
        InitializeComponent();

        MenuItem mi = new MenuItem();
        mi.Header = "Remove";
        mi.Click += Remove_Icon_Click;
        cm1.Items.Add(mi);
        mi = new MenuItem();
        mi.Header += "Replace";
        mi.Click += Replace_Icon_Click;
        cm1.Items.Add(mi);

        mi = new MenuItem();
        mi.Header = "Add Icon";
        mi.Click += Replace_Icon_Click;
        cm2.Items.Add(mi);
    }
    private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
    {
        Camera c = new Camera();
        ContextMenuService.SetContextMenu(c, cm1);
        Homescreen1.Add(c);
        for (int i = 0; i < 19; i++)
        {
            Empty_Icon ei = new Empty_Icon();
            ContextMenuService.SetContextMenu(ei, cm2);
            Homescreen1.Add(ei);
        }

        int row=1, column=1;
        for (int i = 1; i <= Homescreen1.Count ; i++)
        {
            Homescreen1[i - 1].SetValue(Grid.RowProperty, row-1);
            Homescreen1[i - 1].SetValue(Grid.ColumnProperty, column-1);
            grid1.Children.Add(Homescreen1[i - 1]);
            if (i % 4 == 0)
            {
                row++;
                column = 1;
            }
            else
                column++;
        }
    }

Empty_Icon(UserControl)根本不显示上下文菜单。网格中有19个这样的Empty_Icon,但不显示上下文菜单。

我如何在所有这些中使用相同的ContextMenu。如果一个contextmenu只使用一次,它就可以工作。

0 个答案:

没有答案