0

I have created several MenuItems (not in XAML) so i added them the parent item :

( Parent_Menu as MenuItem ).Items.Add( Menu_Item );

Then i applied my styles by VisualTree list (in a loop) . But the styles won't apply on the new MenuItems . Here is my style applying code :

else if ( Visual is MenuItem )
{
    MenuItem menuItem = Visual as MenuItem;
    menuItem.Style = D_Interface.Style_Menu;
}

The parent menuitems got the styles but not the new sub menuitems . So I just checked with the debugger which items are linked to the parent items by the visual tree , And i cannot find the new sub items in the parent items . I guess they are not linked to the parent also in the logical tree .

I checked where the new subitems are in the global visual\logical tree , as i understood from the debugger that they are not linked to any parent not in the visual tree and not in the logical tree and not even to the Window itself .

My conclusion is that they are related to the parent MenuItem only by the "Items" list ... It's not the same like MenuItems are being children of a parent Menu control automatically .

Do anybody have idea how to make the dynamically created MenuItems to be children of the parent in the visual tree ?

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
David von Tamar
  • 797
  • 3
  • 12
  • 29

1 Answers1

1

Objects that are added to the Items collection of an ItemsControl (MenuItem is an ItemsControl) are added as logical children of that ItemsControl. I think the problem is that you are supplying an implicit Style for MenuItem. You are just explicitly setting the Style of the parent but that won't affect the descendants. You can set the ItemContainerStyle and that will affect the direct children (but not their children) or better still just put the Style into the Resources of the MenuItem or one of its ancestors (without setting a key).

AndrewS
  • 6,054
  • 24
  • 31
  • I cannot get the new MenuItem in the loop from the VisualTree .. The problem is that the new MenuItem is not found as a child of his parent . And not to apply the style on the new MenuItem . For example other existing menuItems are found in the VisualTree so their style is applied and that did worked . SO again , The problem is why the new MenuItems are not in the VisualTree of their parent ? ANd how to insert them ? Items.Add does not linking them in VisualTree and i am wondering why ? – David von Tamar Apr 26 '13 at 07:39
  • The children of a MenuItem are displayed within a Popup of the parent menu item so the children are never technically in the same visual tree (they are part of a different HwndSource). You're not saying what you're doing but it sounds like you're walking down the visual tree to affect the elements. If so then you're going to have to walk down the logical tree as well or when you enter a Popup in the visual tree also traverse down into its Child. However I suspect you may still find breaks in the visual tree because typically it is not created until the 1st measure and the template is applied. – AndrewS Apr 26 '13 at 12:38
  • So i should put the style in the Menu_Main's resources ? But anyway , can i get the children of an Menu (all it's items) atleast by a Logical tree loop ? Or make another Helper to enumerate MenuItems (by their .Items collections) ? – David von Tamar Apr 26 '13 at 15:19
  • As I mentioned in my original answer, the things you add to the Items collection of an ItemsControl are in its logical tree so yes you can enumerate the logical tree of any items controls including MenuItem. But yes if you are just trying to define a single Style for MenuItem that affects all the menu items then I would put that style into the Menu or some other ancestor's Resources (without an x:Key). – AndrewS Apr 26 '13 at 16:32