Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF used within a WinForms application, where to put Application resources?

At present we host a number of WPF controls in a WinForms application. The application is started using the System.Windows.Forms.Application.Run(...) method and WPF controls hosted using the ElementHost.

In a normal WPF application I'd define a System.Windows.Application object (App.xaml) and call run on it. Normally any application level WPF resources would go in there. We don't have this.

How can I specify application level resources for the WPF controls but still run as a WinForms app?

like image 772
chillitom Avatar asked Mar 04 '11 09:03

chillitom


People also ask

Can you mix WPF and WinForms?

Yes you can, both Windows Forms within a WPF application, and WPF controls within Windows Forms.

How do I add a resource to WPF?

To add a Resource Dictionary into your WPF application, right click the WPF project > add a Resource Dictionary. Now apply the resource "myAnotherBackgroundColor" to button background and observe the changes.

What is the need of WPF when we had Windows Forms?

WPF allows developers to create event-driven rich client applications for usage on the Windows operating system. WPF can be used to develop and design both Windows applications and web applications while WinForms can only be used to develop and design Windows applications.


1 Answers

In a hosted environment you do not have easy access to the Application, Dr WPF has a couple of methods for working in a hosted scenario at http://drwpf.com/blog/2007/10/05/managing-application-resources-when-wpf-is-hosted/.

I am personally using his SharedResources class in a work project, VB6 Form hosting Winforms UserControl hosting ElementHost hosting WPF UserControl with a Application wide theme, for the WPF controls.

like image 155
benPearce Avatar answered Sep 27 '22 19:09

benPearce