WPF Menu on Winforms - wpf

Is it possible to show WPF Context menu on WinForms?
If yes, how could we do that?

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 :)

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.

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.

Related

XAML, WPF and Windows 8

I'm hoping someone here can explain to me the difference between WPF and XAML exactly in this context:
I have an application (an XBAP specifically) written in VB.NET using MVVM & Repository Pattern, implementing the the usual INotifyPropertyChanged, OLEDB etc etc.
The front end of the application is written in XAML.
From what I understand there is nothing specifically "WPF" about this application. In my view its XAML + VB.NET; where does the WPF come in? Given windows 8 and the "death of Silverlight/WPF" that I keep hearing about, what should I be doing to "upgrade" my application to ensure its future?
I would greatly appreciate someone clearing up this confusion for me.
XAML + your code behind is WPF. You are using WPF. XAML is the markup used for defining the interface in WPF.
And I wouldn't worry too much at this point about "the death of Silverlight/WPF". Just because Windows 8 supports HTML5/JavaScript doesn't mean WPF has gone away. At least, not yet.
See also.
An XBAP (XAML Browser Application) is a kind of WPF XAML application, that runs in the browser. Nothing more, nothing less.
WPF isn't going away anytime soon; in fact, it received a number of enhancements in .NET 4.5. Windows 8 will continue supporting WPF whether it's run on the desktop or as an XBAP. There's no need to "upgrade" your app, but if you want to take advantage of the Windows Store and the new user interface, it's not difficult to port your WPF XAML to WinRT XAML.
See, WPF can be called as the Successor of WinForms . In WPF WE have this XAML that is simply XML but Is more powerful and has greater properties .
WPF isn't dead !
Talking about Windows Store Apps, Silverlight has lots to do with WPF and XAML
Your XBAP application uses WPF : XAML for the UI and VB.NET for the code behind.
If you want to be sure, check if the namespaces you use in the code begin with "System.Windows".
Your XBAP is just a kind of project you can create with the WPF technology. (that's an application which shows several web pages as its UI).

Modal Dialog in WPF

Here is my problem: I am migrating an application from Windows Forms to WPF, but I am totally new in WPF technology.
I have been lucky creating a ribbon also have been able to add some elements to it, but right now I am struggling to create a modal dialog.
How can I achieve this? I have already created all the design of the dialog, but I cannot get it open as a modal window.
Besides, since this application has to be developed under MVVM pattern, where could I place that code? According to what I've read, that code has to be place in the view class, am I right?.
Thank you in advance.
You show a WPF Window modally using Window.ShowDialog. Also this WPF Window Overview will help.
Besides, since this application has to be developed under MVVM
pattern, where could I place that code?
Look under the Related questions to your right, there are many answers to this question in there. For example:
Open dialog in WPF MVVM

Convert Silverlight App to WPF

I need to convert a Silverlight App to WPF (to finally run it on Microsoft Surface 1.0).
I didn't write the original solution and it is quite big. I've never worked with Silverlight and I'm just a little bit familiar with WPF.
Can you offer some advice on completing this project?
Here's my current plan (high level view):
Recreate all Silverlight projects in WPF (User Control in Silverlight = WPF User Control Library ?)
References are not the same so I'll need to make some changes ... I think :)
Hope all controls are compatible with WPF ...
Have I missed something big? Is something wrong? Incomplete?
I'm open to all your suggestions and advice!
Any development tips ?
I recently converted a Silverlight app to WPF. You can find my notes on how I did it here:
http://robertsmyth.blogspot.com.au/2011/12/migrating-from-silverlight-to-wpf.html
Hope it helps you.
"User Control in Silverlight = WPF User Control Library ?" Not exactly.
which version of silverlight do you use? Most difficult issue I have faced while porting WPF to Silverlight was with converters. Silverlight 4.0 or lower doesn't support Ancestor RelativeSource Binding. But supported in Silverlight 5!
Hope this link will help you. WPF and Silverlight 2 Compatibility

How to Design a MVVM UserControl WPF and hosted it into a Windows Forms ElementHost?

i have a some questions about WPF + MVVM + ElementHost. I try to explain it so clear i can.
I'm building an addin(VSTO 2010) that's mean i need an ElementHost to hosted a WPF.
The first Point mean that my WPF can only be an UserControl (WPF)
Regarding Point 1 and 2 ;
It's possible to build an MVVM WPF(UserControl) having Popup(Children) like this One and which can be hosted into a ElementHost?
I hope my Question is enough clear! Thank u for helping.
You sure can, there are a few gotcha's though.
I have had issues with my WPF control not drawing when initially displayed, so I worked around it by tweaking the width when the Child is set. See http://vstocontrib.codeplex.com/SourceControl/changeset/view/50a83624e34d#src%2fVSTOContrib.Core%2fWpf%2fWpfPanelHost.cs
Next is the MVVM style application you want to build, the main issue around this is the VSTO model around windows/documents/custom task panes are all different, one is based on the open workbook (context), one is windows (view) and custom task panes are also window based (view).
MVVM style apps are built more around the current context, or the current opened workbook, I have been working on VSTO contrib for a while now to solve this problem, it even gives you MVVM like bindings when declaring your RibbonXML ribbons if you need ribbon support.
Grab it at http://vstocontrib.codeplex.com and please let me know if it indeed helps you.
And finally the popup, there is nothing stopping you, but you will find in Office 2007 that when you try to open the window for a second time that Office will probably crash. The following code will make your WPF window experience a bit smoother.
if (System.Windows.Application.Current == null)
new Application { ShutdownMode = ShutdownMode.OnExplicitShutdown };
else
System.Windows.Application.Current.ShutdownMode = ShutdownMode.OnExplicitShutdown;
Basically when you display the first window, all is good, but WPF will spin up a Application, which by default exits when the last window is closed. So when your window closes, then you try to open it again, WPF will blow up :P

wpf vs winforms by means of customizeable UI

Which technology (WPF or Winforms) should be used if UI supposed to be highly customizable like controls layout/design could be change by user and such sort of UI customization.
Kindly mention best practices along to achieve that...
I just recently developed a designer in both WinForms (company req) and WPF (to see how much better it was). WPF has a definate edge, especially when it comes to nicer looking controls and control transparency.
This was my first actual WPF project, other than just messing around, so I was learning as I went. I found this series on creating a diagram designer very helpful. I didn't really do the same things that this article talks about, but more of a hybrid between that and my WinForms app.
I have to admit that the UI functionality was up and running much faster in the WPF version than with the WinForms version.
WPF I have found the easiest to create controls on a fly. Because I can just attach them as child controls to the parent, and the Grids, Dock Panels, just make life easier.
I found WinForms to be clunky to always work with. However I come from a Web background and Xaml makes sense to me.
WPF controls are design and lookless. That means you have a default view of them, but everything detail of a WPF control can be overridden. It's almost akin to using CSS. In the WPF world, you do not create custom controls like you do in WinForms. The main thing in WPF world is "styling" controls and defining a style for them. It just happens that the style also controls the layout and the form of the controls.
WPF is FAR superior for designing and style of UI. Check out these two top WPF companies and tell me if this stuff is easy to do in WinForms:
Cynergy Systems: http://www.cynergysystems.com/
Thirteen23: http://www.thirteen23.com/

Resources