Click here to Skip to main content
15,885,435 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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);



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
Posted

1 solution

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900