Domanda

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 ?

È stato utile?

Soluzione

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).

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top