Link to home
Create AccountLog in
Avatar of mojian
mojian

asked on

How Can I get the Event Source where I Click ContextMenu

Below you'll find my basic WPF / C# code to create and show a Context Menu. I currently got a problem I want you help me.

//Create Context Menu
            MenuItem mColorBlack = new MenuItem();
            mColorBlack.Header = "Black";
            mColorBlack.Click += onClickedContextMenuItem;
            MenuItem mColorRed = new MenuItem();
            mColorRed.Header = "Red";
            mColorRed.Click += onClickedContextMenuItem;
            MenuItem mColorBlue = new MenuItem();
            mColorBlue.Header = "Blue";
            mColorBlue.Click += onClickedContextMenuItem;
            MenuItem mColorGreen = new MenuItem();
            mColorGreen.Header = "Green";
            mColorGreen.Click += onClickedContextMenuItem;

            ContextMenu contextmenu = new ContextMenu();
            contextmenu.Items.Add(mColorBlack);
            contextmenu.Items.Add(mColorRed);
            contextmenu.Items.Add(mColorBlue);
            contextmenu.Items.Add(mColorGreen);


                  //Create TextBox
                  TextBox textBox1= new TextBox();
            textBox1.Text = "New TextBox";
            textBox1.AcceptsReturn = true;
            textBox1.TextWrapping = TextWrapping.Wrap;
            TextMenu contextMenu = new TextMenu();
            textBox1.ContextMenu = contextMenu;

//Create TextBox
                  TextBox textBox1= new TextBox();
            textBox1.Text = "New TextBox";
            textBox1.AcceptsReturn = true;
            textBox1.TextWrapping = TextWrapping.Wrap;
            TextMenu contextMenu = new TextMenu();
            textBox1.ContextMenu = contextMenu;

TextBox textBox2= new TextBox();
            textBox2.Text = "New TextBox";
            textBox2.AcceptsReturn = true;
            textBox2.TextWrapping = TextWrapping.Wrap;
            TextMenu contextMenu = new TextMenu();
            textBox2.ContextMenu = contextMenu;


              //ContextMenu Event Handler
        /// <summary>
        ///onClickedContextMenuItem
        /// </summary>
        private void onClickedContextMenuItem(object sender, RoutedEventArgs e)
        {
            //          
        }



WhatI want to do is  when I  chosse the text in the textbox,and right click in the textbox, then Chose a contextmenu item to change color for the selected words;
it will do the onClickedContextMenuItem funtion.
Now I want to Know in onClickedContextMenuItem funtion,How Can I get  the menuItem Name that I clicked.
And which textbox  should be change?

Please help me.



Avatar of parnasso
parnasso
Flag of Italy image

Check the Source property of the RoutedEventArgs class object.

If it were a MenuItem (check the type first) you could do like this:

MenuItem srcMenu = (MenuItem )e.Source;



When I said "check the type first", I meant to ensure that the source object is a MenutItem object type before casting. Like:

MenuItem srcMenu;
if(e.Source.GetType() == typeof(MenuItem))
{
  srcMenu = (MenuItem )e.Source;
}


Avatar of mojian
mojian

ASKER

Thanks for your reply.
But in that way ,just get a srouce menu,which can not get a menuitem's name from it
I means i t can not get the name of the menuitem from srcMenul

Pls help me.
Avatar of mojian

ASKER

if i use srcMenu.Name to get the name of srcMenu,it returns  nothing.
Avatar of mojian

ASKER

Oh,I get it ,
I forget to set the Name for MenuItem...

thanks for your help
ASKER CERTIFIED SOLUTION
Avatar of parnasso
parnasso
Flag of Italy image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of mojian

ASKER

just get a partialy solution