Parent & Child menitems - WPF .NET 8.0

I'm struggling with WPF MenuItems. Specifically, I need to open a window when the "File" parent MenuItem is clicked, but not when any of its child MenuItems are clicked. Despite trying several methods, including using tags, combining tags with click events, and relying solely on click events, nothing has worked.
<ContextMenu>
<MenuItem Header="File" Click="FileManager_Click" >
<MenuItem Header="New" >
<MenuItem Header="Open" />
<MenuItem Header="Save" />
</MenuItem>
</MenuItem>
</ContextMenu>
<ContextMenu>
<MenuItem Header="File" Click="FileManager_Click" >
<MenuItem Header="New" >
<MenuItem Header="Open" />
<MenuItem Header="Save" />
</MenuItem>
</MenuItem>
</ContextMenu>
2 Replies
SparkyCracked
SparkyCracked9mo ago
This is done using the backend code, the C# code
c#
private void MenuItem_Click(object sender, RoutedEventArgs e)
{
if (e.OriginalSource == sender)
{
var window = new TheWindowYouWannaOpen();
window.Show();
}
}
c#
private void MenuItem_Click(object sender, RoutedEventArgs e)
{
if (e.OriginalSource == sender)
{
var window = new TheWindowYouWannaOpen();
window.Show();
}
}
Here we have you event, in this case click, it will then check if the clicked item is parent of MenuItem and then it opens your window, whatever it is called
Xeinٴٴٴٴٴٴٴ
XeinٴٴٴٴٴٴٴOP9mo ago
Have already tried, it doesn't work.
Want results from more Discord servers?
Add your server