Prashant Cholachagudda
Prashant Cholachagudda

Reputation: 13092

WPF Menu on Winforms

Is it possible to show WPF Context menu on WinForms?

If yes, how could we do that?

Upvotes: 4

Views: 1183

Answers (3)

Timothy Khouri
Timothy Khouri

Reputation: 31845

OK, so I've found the answer myself... (I tried to give a 50 point bounty, but I don't think I can award myself :P)

We have a lot of legacy WinForms... forms... still in our project at work. And here's the secret code that is needed to display a WPF Context Menu in a WinForms project!

var myMenu = new System.Windows.Controls.ContextMenu();
myMenu.Items.Add("One");
myMenu.Items.Add("Two");
myMenu.Items.Add("Three");
myMenu.IsOpen = true;

Tada!!! - There is no secret code needed... you can write this code in a WPF App or a WinForms app and it works the same.

And there's your answer :)

Upvotes: 3

aqwert
aqwert

Reputation: 10789

You can mix the 2 technologies using WindowsFormsHost (WinForm inside WPF window) and ElementHost (WPF inside WinForm window).

You will not encounter the Airspace issue as you are adding a WPF menu into a Winform application. But you will if you create WPF windows with Winform UserControls. This Blog post has some ideas about solving it, but it is not pretty.

Upvotes: 2

Muad'Dib
Muad'Dib

Reputation: 29226

I don't know if it can be done, but this article might help point you in the right direction. As a WPF developer, it seems strange to me to want to mix winforms and wpf, but i can see the case for re-use.

Upvotes: 0

Related Questions