添加菜单项当项目来源设置为菜单项时 [英] Add menuitem When item source set to menuitem
本文介绍了添加菜单项当项目来源设置为菜单项时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好,
我已经创建了上下文菜单并将其子菜单项设置为项目源,但是现在我想再添加一个菜单项作为"Default".我将异常抛出为
使用ItemsSource之前,Items集合必须为空.
以下是我的代码
Hello ,
I have created context menu and set its submenu item to an item source,But now I want to add one more menuitem as "Default".I do that exception is thrown as
Items collection must be empty before using ItemsSource.
Following is my code
MenuItem Menu1 = new MenuItem();
Menu1.Header = "Menu1";
MenuItem Menu2 = new MenuItem();
Menu2.ItemsSource = to some object collection;
Menu2.DisplayMemberPath = "Name";
Menu1.Items.Add(Menu2);
现在,我想在Menu2和itemcollection中添加另一个Item
已经尝试过
Now I want to add one more Item in Menu2 and the itemcollection
Already tried
MenuItem DefaultMenu = new MenuItem();
DefaultMenu.Header = "Default";
Menu2.Items.Insert(0, Default);
引发异常
有什么简单的方法可以添加另一个菜单项,该菜单项的来源已经固定
在此先谢谢您
Throws exception
Is there any easy way to add another menuitem whose item source is already fixed
Thanks in advance
推荐答案
您可以将项目添加到设置为ItemsSource
的集合中,也可以创建一个包含原始项目的新集合集合和其他项.
You can either add an item to the collection that is set as the ItemsSource
or, create a new collection that contains the items of the original collection and the additional item.
这篇关于添加菜单项当项目来源设置为菜单项时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文