S
ssg31415926
I've got a ListView which can show many different types of object. I
need to display a ContextMenu whose MenuItems depend on the object
type. I was planning to pre-create the ContextMenus when the app is
started and then assign them when the object is clicked on. The first
few items in the menus
will all be the same (Large Icons, Small Icons, etc.).
I create all of those objects, then add them to the first context menu
and all is well. Then I add them to the second context menu and the
first one has zero items. I don't understand why. Why can't a
MenuItem appear on two different ContextMenus?
Here is part of my code. The first foreach loop returns no items. The
second returns the list of MenuItems I'd expect. Can anyone explain
why and how I avoid this. I didn't want to create specific instances
of each MenuItem for each context menu because I wanted, for example,
to store the currently selected View (Details, Small Icons, etc.) in
the appropriate MenuItem.
private ContextMenu m_DefaultContextMenu;
private ContextMenu m_ComputerContextMenu;
private ContextMenu m_UserContextMenu;
private ContextMenu m_GroupContextMenu;
private MenuItem m_cmnuListViewLargeIcons;
private MenuItem m_cmnuListViewSmallIcons;
private MenuItem m_cmnuListViewDetails;
private MenuItem m_cmnuListViewList;
private MenuItem m_cmnuListViewSeparator1;
private MenuItem m_cmnuListViewBrowseToScript;
private MenuItem m_cmnuListViewSeparator2;
this.m_cmnuListViewBrowseToScript = new MenuItem("&Browse to
script...");
this.m_cmnuListViewBrowseToScript.Index = 0;
this.m_cmnuListViewSeparator1 = new MenuItem("-");
this.m_cmnuListViewSeparator1.Index = 1;
this.m_cmnuListViewLargeIcons = new MenuItem("Lar&ge Icons", new
System.EventHandler(this.mnuViewLargeIcons_Click));
this.m_cmnuListViewLargeIcons.Index = 2;
this.m_cmnuListViewSmallIcons = new MenuItem("S&mall Icons", new
System.EventHandler(this.mnuViewSmallIcons_Click));
this.m_cmnuListViewSmallIcons.Index = 3;
this.m_cmnuListViewDetails = new MenuItem("&Details", new
System.EventHandler(this.mnuViewDetails_Click));
this.m_cmnuListViewDetails.Index = 4;
this.m_cmnuListViewList = new MenuItem("&List", new
System.EventHandler(this.mnuViewList_Click));
this.m_cmnuListViewList.Index = 5;
this.m_cmnuListViewSeparator2 = new MenuItem("-");
this.m_cmnuListViewSeparator2.Index = 6;
this.m_cmnuListViewDetails.Checked = true;
this.m_DefaultContextMenu = new ContextMenu(new MenuItem[] {
this.m_cmnuListViewBrowseToScript,
this.m_cmnuListViewSeparator1,
this.m_cmnuListViewLargeIcons,
this.m_cmnuListViewSmallIcons,
this.m_cmnuListViewDetails,
this.m_cmnuListViewList,
this.m_cmnuListViewSeparator2 } );
this.m_UserContextMenu = new ContextMenu(new MenuItem[] {
this.m_cmnuListViewBrowseToScript,
this.m_cmnuListViewSeparator1,
this.m_cmnuListViewLargeIcons,
this.m_cmnuListViewSmallIcons,
this.m_cmnuListViewDetails,
this.m_cmnuListViewList,
this.m_cmnuListViewSeparator2 } );
Debug.WriteLine("Post-initialize check on Default");
foreach (MenuItem mi in this.m_DefaultContextMenu.MenuItems) {
Debug.WriteLine("MenuItem = " + mi.Text);
}
Debug.WriteLine("Post-initialize check on User:");
foreach (MenuItem mi in this.m_UserContextMenu.MenuItems) {
Debug.WriteLine("MenuItem = " + mi.Text);
}
need to display a ContextMenu whose MenuItems depend on the object
type. I was planning to pre-create the ContextMenus when the app is
started and then assign them when the object is clicked on. The first
few items in the menus
will all be the same (Large Icons, Small Icons, etc.).
I create all of those objects, then add them to the first context menu
and all is well. Then I add them to the second context menu and the
first one has zero items. I don't understand why. Why can't a
MenuItem appear on two different ContextMenus?
Here is part of my code. The first foreach loop returns no items. The
second returns the list of MenuItems I'd expect. Can anyone explain
why and how I avoid this. I didn't want to create specific instances
of each MenuItem for each context menu because I wanted, for example,
to store the currently selected View (Details, Small Icons, etc.) in
the appropriate MenuItem.
private ContextMenu m_DefaultContextMenu;
private ContextMenu m_ComputerContextMenu;
private ContextMenu m_UserContextMenu;
private ContextMenu m_GroupContextMenu;
private MenuItem m_cmnuListViewLargeIcons;
private MenuItem m_cmnuListViewSmallIcons;
private MenuItem m_cmnuListViewDetails;
private MenuItem m_cmnuListViewList;
private MenuItem m_cmnuListViewSeparator1;
private MenuItem m_cmnuListViewBrowseToScript;
private MenuItem m_cmnuListViewSeparator2;
this.m_cmnuListViewBrowseToScript = new MenuItem("&Browse to
script...");
this.m_cmnuListViewBrowseToScript.Index = 0;
this.m_cmnuListViewSeparator1 = new MenuItem("-");
this.m_cmnuListViewSeparator1.Index = 1;
this.m_cmnuListViewLargeIcons = new MenuItem("Lar&ge Icons", new
System.EventHandler(this.mnuViewLargeIcons_Click));
this.m_cmnuListViewLargeIcons.Index = 2;
this.m_cmnuListViewSmallIcons = new MenuItem("S&mall Icons", new
System.EventHandler(this.mnuViewSmallIcons_Click));
this.m_cmnuListViewSmallIcons.Index = 3;
this.m_cmnuListViewDetails = new MenuItem("&Details", new
System.EventHandler(this.mnuViewDetails_Click));
this.m_cmnuListViewDetails.Index = 4;
this.m_cmnuListViewList = new MenuItem("&List", new
System.EventHandler(this.mnuViewList_Click));
this.m_cmnuListViewList.Index = 5;
this.m_cmnuListViewSeparator2 = new MenuItem("-");
this.m_cmnuListViewSeparator2.Index = 6;
this.m_cmnuListViewDetails.Checked = true;
this.m_DefaultContextMenu = new ContextMenu(new MenuItem[] {
this.m_cmnuListViewBrowseToScript,
this.m_cmnuListViewSeparator1,
this.m_cmnuListViewLargeIcons,
this.m_cmnuListViewSmallIcons,
this.m_cmnuListViewDetails,
this.m_cmnuListViewList,
this.m_cmnuListViewSeparator2 } );
this.m_UserContextMenu = new ContextMenu(new MenuItem[] {
this.m_cmnuListViewBrowseToScript,
this.m_cmnuListViewSeparator1,
this.m_cmnuListViewLargeIcons,
this.m_cmnuListViewSmallIcons,
this.m_cmnuListViewDetails,
this.m_cmnuListViewList,
this.m_cmnuListViewSeparator2 } );
Debug.WriteLine("Post-initialize check on Default");
foreach (MenuItem mi in this.m_DefaultContextMenu.MenuItems) {
Debug.WriteLine("MenuItem = " + mi.Text);
}
Debug.WriteLine("Post-initialize check on User:");
foreach (MenuItem mi in this.m_UserContextMenu.MenuItems) {
Debug.WriteLine("MenuItem = " + mi.Text);
}