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(mCol orBlack);
contextmenu.Items.Add(mCol orRed);
contextmenu.Items.Add(mCol orBlue);
contextmenu.Items.Add(mCol orGreen);
//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>
///onClickedContextMenuIte m
/// </summary>
private void onClickedContextMenuItem(o bject 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.
//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(mCol
contextmenu.Items.Add(mCol
contextmenu.Items.Add(mCol
contextmenu.Items.Add(mCol
//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>
///onClickedContextMenuIte
/// </summary>
private void onClickedContextMenuItem(o
{
//
}
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.
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;
}
MenuItem srcMenu;
if(e.Source.GetType() == typeof(MenuItem))
{
srcMenu = (MenuItem )e.Source;
}
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.
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.
ASKER
if i use srcMenu.Name to get the name of srcMenu,it returns nothing.
ASKER
Oh,I get it ,
I forget to set the Name for MenuItem...
thanks for your help
I forget to set the Name for MenuItem...
thanks for your help
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
just get a partialy solution
If it were a MenuItem (check the type first) you could do like this:
MenuItem srcMenu = (MenuItem )e.Source;