Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

C# - ContextMenu Opening Event Not Firing

$
0
0

Currently, I'm Hosting a WPF TextBox Control using ElementHost in my Windows Forms project however I've encountered an error regarding the ContextMenu for the WPF TextBox. Basically Regardless of what Items I add, The Context Menu doesn't show the added Items. I further tested this by adding an Event to the TextBox ContextMenuOpening yet nothing happens.

My Code Follows:

Accessing TextBox from ElementHost ony my Main Form:

System.Windows.Controls.TextBox _TextBox = new System.Windows.Controls.TextBox();
this.elementHost1.Child = _TextBox;

XAML regarding the TextBox control as UserControl. This is the control placed inside the ElementHost in my Main Form:

<TextBox Name="txtEdit" SpellCheck.IsEnabled="True"  AcceptsReturn="True" VerticalScrollBarVisibility="Visible" ContextMenuOpening="txtEdit_ContextMenuOpening"><TextBox.ContextMenu><ContextMenu x:Name="ctxMenu"></ContextMenu></TextBox.ContextMenu></TextBox>
 void txtEdit_ContextMenuOpening(object sender, ContextMenuEventArgs e)
 {
    MessageBox.Show("Event Fired!");
 }

Even adding the event handlers programatically within the TextBox InitializeComponent(); didn't work.

txtEdit.ContextMenuOpening += txtEdit_ContextMenuOpening;

Here's how I'd usually add Items to the ContextMenu:

void txtEdit_ContextMenuOpening(object sender, ContextMenuEventArgs e)
        {
            int index = 0;
            Separator seperator2 = new Separator();
            this.txtEdit.ContextMenu.Items.Insert(index, seperator2);
            index += 1;

            MenuItem searchMenuItem = new MenuItem();
            searchMenuItem.Header = "Search Google";
            this.txtEdit.ContextMenu.Items.Insert(index, searchMenuItem);
            index += 1;


            MenuItem defineMenuItem = new MenuItem();
            defineMenuItem.Header = "Define";
            this.txtEdit.ContextMenu.Items.Insert(index, defineMenuItem);
            index += 1;

            Separator seperator22 = new Separator();
            this.txtEdit.ContextMenu.Items.Insert(index, seperator22);
            index += 1;
        }

What could be the problem?


MessageBox.Show("Hey You!")




Viewing all articles
Browse latest Browse all 18858

Trending Articles