is it possible to use Winform components in WPF - wpf

We have a product which is developed using Winforms - .net 2.0. Now we are thinking about migrating this application to WPF. Is is possible to use the same Winform components in WPF.
Or if it is not possible, then which is best possible ways to Migrate this application to WPF.

Yes. You can use ElementHost to put WPF content into Windows Forms controls, and WindowsFormsHost to host your Windows Forms component directly within a WPF element.

There's a tutorial on Switch On The Code using the DataGridView.
You need to add references to System.Windows.Forms and WindowsFormsIntegration to get WindowsFormsHost.
You can do it in the XAML as well as the C# code:
<WindowsFormsHost Grid.Row="0">
<WinForms:DataGridView x:Name="_MyDataGrid">
</WinForms:DataGridView>
</WindowsFormsHost>
C#
_MyHost = new WindowsFormsHost();
_MyDataGrid = new System.Windows.Forms.DataGridView();
....
_MyHost.Child = _MyDataGrid;
_Container.Children.Add(_MyHost);

Related

Does the new SvgImageSource class designed for UWP can be used in WPF project

It seems that a relatively new class: SvgImageSource can only be used in UWP. Is there any way to use it in a WPF project and how?
Please take a look at this framework:
SharpVectors
The other way is to convert your SVG files to XAML path geometry and using them easily.
It will be possible to use any UWP XAML UI in WPF using XAML Islands. This is currently available as a preview using the WindowsXamlHost control which is part of the Windows Community Toolkit. Also note, that when using UWP controls in WPF apps, the app will then work only on Windows 10 client PCs.

Winforms toolbox tools for WPF

I'm new to WPF and I'm wondering is there anyway to have elements from Winforms toolbox in WPF. I mean the Winforms toolbox has a lot of elements and they're not present in WPF toolbox. for example something like PerformanceCounter in Winforms toolbox is not found in WPF toolbox.
thanks in advance
There are some controls that didn't get represented in WPF, but you can use WindowsFormsHost. As the name indicates, it's purpose is to host the Windows Forms elements.
Walkthrough: Hosting a Windows Forms Control in WPF
I would add that things like PerformanceCounter you can use in code, rather than placing it on a form. Other elements which do have a UI purpose can be placed inside a WindowsFormsHost control. I do this with ReportViewer -- it's a Windows Forms control that has no WPF equivalent.

WPF forms+ Winforms in MDI

We have an application which is using winforms. Now we want to upgrade it.
We are planning to use some WPF forms and some old winforms. and we also need to use MDI.
From MDIParent we need to open both winforms and WPF forms. And these forms need to be in tile format. I got to know that WPF doesn't support MDI.
Is there any other way to achieve this?
Using AvalonDock, can we display the forms in tiles format also. I know that it display in Dockable format.
why you want to upgrade your application to wpf and still use some of your old winforms and the mdi concept any further?(i dont like mixup both:))
for quick and dirty what about using your winform app and integrate wpf in it - if you want/have to?
EDIT:
if you want your WinForms in Wpf you can use
<WindowsFormsHost />
It should work with Avalon too. Here is an example for WindowsFormsHost.

Mixing WPF with a WinForm application?

My fairly large WinForm application needs a GUI overhaul, but I can't afford to do it all at once. I need to know if I can slowly add WPF into it, and if so, how?
Can I add WPF dialogs?
Can I add WPF 'panels' within a WinForm so that I can embed WPF elements?
EDIT
Can I do the opposite and put WinForm dialogs in my WPF application?
Yes, you can host WPF controls in your WinForms applications:
Walkthrough: Hosting a 3-D WPF Composite Control in Windows Forms
Walkthrough: Hosting a Composite WPF Control in Windows Forms
Yep, I've successfully mixed winforms and WPF. I even managed to add WPF windows to win32 apps, changed this app to a dll and used a WPF app to show win32 windows which show WPF windows.
To host WPF windows in WinForm or win32 apps you will need this line befor you .Show() your WpfWindow:
System.Windows.Forms.Integration.ElementHost.EnableModelessKeyboardInterop(myWpfWindow);
See http://msdn.microsoft.com/en-us/library/aa348549.aspx
You can use the ElementHost control found in the System.Windows.Forms.Integration namespace. You will need a reference to the WindowsFormsIntegration assembly (in WindowsFormsIntegration.dll)
This control is an empty container into which you can put WPF controls e.g.
myElementHost.Child = someWpfControl;
You can find it in your toolbox and drag and drop it onto a winform like any other control;

Using MS ReportViewer in WPF

I'm about to start using the MS ReportViewer in a WPF application by placing the ReportViewer in a WindowsFormsHost.
Is this the correct approach? What is the best way of designing the reports as you cannot use the ReportViewer at design time in a WindowsFormsHost.
Is it best to create an RDL report in SQL Server and then convert it to an RDLC or maybe to create a new Winforms app to create an RDLC file in a WinForms framework and then copy it to the WPF app.
I will need to filter the reports via dropdowns so there's that aspect to consider too. If anyone out there is already using ReportViewer in WPF I would appreciate some feedback on the best approach.....Many thanks.
Yes, that works, I am using the WindowsFormsHost in a wpf project to wrap the ReportViewer.
In the ViewModel I am creating the WindowsFormsHost and the ReportViewer:
WindowsFormsHost windowsFormsHost = new WindowsFormsHost();
reportViewer = new ReportViewer();
windowsFormsHost.Child = reportViewer;
this.Viewer = windowsFormsHost
and in the View I am using a ContentPresenter to display it, by binding to the Property that holds the WindowsFormsHost.
<ContentPresenter Content="{Binding Viewer}" ...
We're using the Business Intelligence Studio (which is an Visual Studio 2008 with templates for editing reports) for report creation. http://msdn.microsoft.com/en-us/library/ms173767.aspx
Take care,
Martin
We've definitely had success just using the WindowsFormsHost. I haven't been involved in creating the RDLC files themselves, but I believe they were designed (as you say) in a WinForms project and then copied across.
Note that if you don't need local reports you can use a WPF Frame control and point it at the URL of the server-based report (it renders it like a web browser would). This works really well for us too.
Please note that you can use both WPF-Windows and Windows-Form-Windows in the same application.
So you can avoid using WindowsFormsHost if you put the ReportViewer in a seperate Windows-Forms-Window that you open from your WPF-Window with ShowDialog.
Then you can use the ReportViewer also at design time.

Resources