How to use a ContextMenu with a TextBox MenuItem?

Posted on 2010-03-29
Medium Priority
Last Modified: 2013-11-12
Below you'll find my basic WPF / C# code to create and show a Context Menu. I currently got two problems I want to address here.

1.) The TextBox appears in the ContextMenu. But whenever I click it (to enter a text) the context menu disappears (like when clicking normal context menu entries. But it should just close when my Cursor is in the Box and I hit the 'Enter' button or when I leave the TextBox it should behave like a normal context menu (closed when clicking on main window...)

2.) Whenever the text of the TextBox is changing I would like to update Item2 - to a text like the text of the TextBox + additional text. E.g. if you enter "Hello" in the TextBox, Item2 changes from "Entry2" to "Hello World". This should happen on every key you enter so at first it would be "H World" after entering the second key "He World" and so on...

System.Windows.Controls.ContextMenu Menu = new System.Windows.Controls.ContextMenu ( );
System.Windows.Controls.MenuItem      Item1      = new System.Windows.Controls.MenuItem ( );      
System.Windows.Controls.MenuItem      Item2      = new System.Windows.Controls.MenuItem ( );      
System.Windows.Controls.TextBox      Box      = new System.Windows.Controls.TextBox ( );
Box.AcceptsReturn                  = true;
Box.MaxLength                  = 25;      
Box.Text                              = "Text to edit";                              
Item1.Header                        = Box;
Item2.Header                        = "Entry 2";
Menu.Items.Add                  ( Item1 );
Menu.Items.Add                  ( Item2 );
Menu.IsOpen                        = true;
Question by:FlorianStruck
  • 2
LVL 16

Accepted Solution

CuteBug earned 1500 total points
ID: 28968123

Author Comment

ID: 28975118
I would like to stick to a ContextMenu if possible.

I just disovered when I set Menu.IsCheckable = true that I at least can insert a new text (when my mouse is exactly over the TextBox), but of course a 'Checked' Arrow appears left of my TextBox.

Author Closing Comment

ID: 31708261
I did is a popup now, thanks!

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
If you are looking for an automated solution for backup single or multiple Office 365 user mailboxes to Outlook data file, then you can use Kernel Office 365 Backup & Restore tool. Go through the video to check out the steps to backup single or mult…
Suggested Courses

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question