Resolved How to set click event for primary menu items in xaml?

destro

Well-known member
Joined
Mar 28, 2020
Messages
46
Programming Experience
1-3
My Menu is perfectly bound with observable collection in view model and all submenu items fires click event properly using below xaml, but I am unable to set the same click event for first menu items. Is it possible to set same click event on all primary menu items?

UserControl.xaml:
<Menu Background="Lavender" x:Name="Styled2" Margin="162,-46,146,335"  Grid.ColumnSpan="2">           
            <MenuItem ItemsSource="{Binding LaalKitabYears}" Height="21" Width="124" HorizontalContentAlignment="Center" Padding="0">
                <MenuItem.Header>
                    <Label VerticalContentAlignment="Top" Margin="0,0,-120,0" HorizontalAlignment="Stretch" HorizontalContentAlignment="Center" Padding="0" Content="{Binding LaalKitabYears[0].year}"/>
                </MenuItem.Header>
            <MenuItem.ItemContainerStyle>
                <Style TargetType="{x:Type MenuItem}">
                    <Setter Property="Header" Value="{Binding Path=year}"/>
                    <Setter Property="ItemsSource" Value="{Binding subMenuItems}"/>
                    <EventSetter Event="Click" Handler="MenuItem_Click"/>
                    <Setter Property="Background" Value="LavenderBlush"/>
                    </Style>
            </MenuItem.ItemContainerStyle>
            </MenuItem>
        </Menu>

This is my menuClickEvent in UserControl.xaml.cs:

UserControl.xaml.cs:
  private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show((e.OriginalSource as MenuItem).Header.ToString());
        }

3Ba27Bsq0l.gif
 
I'm not sure I understand the question.

Are you trying to make it so 1995, 2005, 2010 are all clickable to produce a date?

Sorry, but you will need to be very specific so that I know exactly what you are trying to do.
 
I'm not sure I understand the question.

Are you trying to make it so 1995, 2005, 2010 are all clickable to produce a date?

Sorry, but you will need to be very specific so that I know exactly what you are trying to do.
Exactly u got it right.. e.g. 2010 fires the click event as it is submenu but 1995, 2005, other parent menuItems are not showing same behaviour.
 
Here's a more complete example that you can copy and paste to play with...
MainWindow.xaml.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Drawing;

namespace SimpleWpf
{
    public class YearMenu
    {
        public int Year { get; set; }
        public List<YearMenu> SubMenuItems { get; set; }

        public YearMenu(int year, params YearMenu [] subMenuItems)
        {
            Year = year;
            SubMenuItems = subMenuItems?.ToList();
        }
    }

    public partial class MainWindow : Window
    {
        public List<YearMenu> YearsMenu { get; set; }

        public MainWindow()
        {
            YearsMenu = new List<YearMenu>()
            {
                new YearMenu(1995,
                    new YearMenu(1996),
                    new YearMenu(1997),
                    new YearMenu(1998),
                    new YearMenu(1999),
                    new YearMenu(2000),
                    new YearMenu(2001),
                    new YearMenu(2002),
                    new YearMenu(2003),
                    new YearMenu(2004),
                    new YearMenu(2005)
                ),
                new YearMenu(2005,
                    new YearMenu(2006),
                    new YearMenu(2007),
                    new YearMenu(2008),
                    new YearMenu(2009),
                    new YearMenu(2010),
                    new YearMenu(2011),
                    new YearMenu(2012),
                    new YearMenu(2013),
                    new YearMenu(2014),
                    new YearMenu(2015)
                ),
                new YearMenu(2015,
                    new YearMenu(2016),
                    new YearMenu(2017),
                    new YearMenu(2018),
                    new YearMenu(2019),
                    new YearMenu(2020),
                    new YearMenu(2021),
                    new YearMenu(2022),
                    new YearMenu(2023),
                    new YearMenu(2024),
                    new YearMenu(2025)
                )
            };

            InitializeComponent();
            DataContext = this;
        }

        void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show((e.OriginalSource as MenuItem).Header.ToString());
        }
    }
}

MainWindow.xaml:
<Window x:Class="SimpleWpf.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Send Image" Height="450" Width="625">
    <Grid>
        <Menu Background="Lavender" x:Name="Styled2">
            <MenuItem ItemsSource="{Binding YearsMenu}" HorizontalContentAlignment="Center" >
                <MenuItem.Header>
                    <Label VerticalContentAlignment="Top" HorizontalAlignment="Stretch" HorizontalContentAlignment="Center" Content="{Binding YearsMenu[0].Year}"/>
                </MenuItem.Header>
                <MenuItem.ItemContainerStyle>
                    <Style TargetType="{x:Type MenuItem}">
                        <Setter Property="Header" Value="{Binding Path=Year}"/>
                        <Setter Property="ItemsSource" Value="{Binding SubMenuItems}"/>
                        <EventSetter Event="Click" Handler="MenuItem_Click"/>
                        <Setter Property="Background" Value="LavenderBlush"/>
                    </Style>
                </MenuItem.ItemContainerStyle>
            </MenuItem>
        </Menu>
    </Grid>
</Window>
 
haha. Thanks Skydiver. I was trying to work the OP's brains to workout where to add the event handler for the new click event. :)

Also, OP wanted to know how to do it in Xaml.
 
Yes, my code above does exactly the same. I was just put in some code to make it easier for others to try to reproduce the problem by just copying and pasting instead of making people jump through hoops to even just get to the point of reproducing the issue.

Any was a big hint as to why the two routed click events are showing the same text: The handler is always using OriginalSource. Since the original source of the event is clicking on the leaf menu item, that is what is always displayed.
 

Latest posts

Back
Top Bottom