WPF User Control in WinFORMS: Project Structuring - c#

SCENARIO
I decided to work on WPF technology for my new application. This application has to be called on Menu click from a WinForms window. So I created a WPF UserControl Library and integrate it to display in parent WinForms Form using Element Host.
My Application
It contains 3 child usercontrols which are encapsulated inside another usercontrol with tabContent Control. I prefered this approach as firing events from Child UserControl and handling in WinForms (subscribing events) seemed painful.
My Question
Now facing the same painful task of accessing UserControl elements inside Winforms where I have created Data Manager class for proper project structuring reasons (UserControl should not contain Data Manager class-UI). Please guide me as to how to structure my project/how to subscribe events/access WPFUserControl elements inside WPF.

Take a look at the Messenger class of MVVM Light Toolkit (can also be used standalone). It helps decoupling your controls. The messenger works with a publish/subscribe pattern. Your WPF UserControls can publish objects, the WinForms Host can listen on those notifications. The exchanged messages (objects) are best placed in separate assembly, as they define the shared contract between WPF UC-library and WinForms application.

Related

Embed threaded WindowsFormsHost in WPF

I have a WPF application and an ActiveX control that gets embedded into the WPF application using a WindowsFormsHost control.
The ActiveX control is used to communicate with an external application that draws 3D contents into the control.
One big issue of this setup is that if the ActiveX control does some long running operations, the rest of the WPF application gets blocked.
I would like to solve this issue and somehow prevent the ActiveX control to be hosted on the same thread as the remaining controls in my UI.
I searched for possible solutions and found this article:
https://blogs.msdn.microsoft.com/dwayneneed/2007/04/26/multithreaded-ui-hostvisual/
Unfortunately I cannot use this sample to host a WindowsFormsHost element as it is not derived from Visual.
The only other option I could implement so far is to host the ActiveX control inside a separate window which I launch on a separate thread.
Although this works it is quite messy to manage as I have to manually snap the separate window to the main window to get kind of a uniform layout.
Is there anything else I can try to achieve a single-window experience but also having the ActiveX control hosted on a separate thread?

Winform control code placement

I'm adding a bar manager and popup menu control to a Winforms application. I have to add the code to bind the menu to the manager, but I don't know where it would be best to do so.
I'll be using the designer heavily (company mandated) for the rest of the build-out, but the binding has to be done in code AFAIK. Currently I have it in the form load method.
I believe this is just fine to make it work, but I'm curious if you could put it in the designer code with the control details, or if it should go somewhere else in the code behind.
Hopefully this isn't an opinion based question.
Winforms doesn't make it very easy to separate things correctly, but you should try to separate as much as possible the UI code from the functional code.
Basically, you should try to put all your business logic in classes that are separated from your UI. Try to think that all that code could be used by another type of application, like a web app, or a WPF app.
The things that are in the codebehind should be only related to UI management, updating the UI and passing the changes to your business classes. There also seems to be some things that exist to have a MVVM or MVP on Winforms, check this SO question: UI Design Pattern for Windows Forms (like MVVM for WPF).

MVVM for third party WPF control

We are using a third party Chart control which was developed using WPF and that doesn't support MVVM. Now we need to add few more functionality and that should support Add/Remove feature on demand. We need to create Wrapper Chart control with new features and that should support MVVM. This dll/Module will be used by a few other Modules/Projects.
Note :
For every new feature we created Handler and composed the existing chart control. So we were able to support add/remove feature on demand.
Question :
How to support MVVM, is it for each FeatureHandler should i create ViewModel which wrap FeatureHandler(View) and looks like MVVM? Need clarification on this part.....
What do you mean dosen't support MVVM? I sounds fairly unlikely that a chart control developed with WPF dosen't expose properties in form of dependency properties. Do you have link to this third party chart control?

Own controls similar to TextBox in C# Windows application

Create own TextBox, Button etc control as own control using User control in C# Windows application, is this good idea?
I wanted make consistency for through out the application. Suppose if I want to change the Textbox border color then all forms textbox updated with this changes. It's just an example.
Please suggest me.
I don't recommend using UserControl just for consistency. If application skinning is what you are after, look into WPF. It makes it relatively simple to skin an application (or even a window, or smaller groups)
Here is an article on skinning with WPF: http://www.codeproject.com/Articles/19782/Creating-a-Skinned-User-Interface-in-WPF
Another alternative, staying within Windows Forms, is creating a class that inherits from TextBox, and using that class throughout the application. The Factory pattern would work well here. You could even adapt it to multiple skins.
It's not a bad idea to provide custom controls that match your "User Experience" (UX). It really just depends on what you are trying to accomplish with your program.

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?
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.
If you host WPF controls within a WinForms application you do not have the Applicationobject which hosts the application-wide resources. The trick is to create such a object, load your global resources and merge them into the ResourceDictionary.
Here is an example of this code:
http://www.snippetsource.net/Snippet/26/load-application-level-resources-in-winforms-hosted-wpf-controls (Link fixed)

Categories