Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add menu items in specific position in WPF?

Tags:

c#

wpf

I want to add a menu item like this:

AddChild(new MenuItem
{
    Header = "Test"
});

However, the problem with that is that the menu item appears in the end (logically), now I'm looking for a way to add it to a specific position.

--A--
--B--
--C--

I want to add it between A and B. The XAML is like:

<ContextMenu>
    <MenuItem ... />
    <MenuItem ... />
    <MenuItem ... />
</ContextMenu>

The code runs in context of the context menu. And I can access the existing menu items by their name A, for example.

like image 267
Tower Avatar asked Dec 29 '25 20:12

Tower


1 Answers

Try

myContextMenu.Items.Insert( newIndex, new MenuItem() {Header="Test"});
like image 58
dowhilefor Avatar answered Dec 31 '25 10:12

dowhilefor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!