0

I have a list view with a context menu. I am looking to dynamically add an item which has a click event. As you can see below, doing it statically is pretty straight forward in the XAML. However, when I try to do this in C# it will not compile. I've checked several existing StackOverflow questions, but they seem to be using different controls or are for WinForms instead.

<ListView x:Name="listView" Margin="10,7,10,8">
    <ListView.ContextMenu>
        <ContextMenu>
            <MenuItem x:Name="Test" Header="Test" Click="Test"/>
        </ContextMenu>
    </ListView.ContextMenu>
<ListView.View>

ModuleName is the name and DisplayModule() is the click handler event I want.

enter image description here

1 Answer 1

2

Can you try this?

var menuItem = new MenuItem();
menuItem.Name = ModuleName;
menuItem.Header = null;
menuItem.Click += DisplayModule;
listView.ContextMenu.Items.Add(menuItem);
Sign up to request clarification or add additional context in comments.

1 Comment

I actually had tried doing that, but I was doing it like this imgur.com/a/Iz3BNMu And it wouldn't let me add the Click. But declaring it how you did allowed me to access the Click property. Seems to work!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.