Skip to content

Using WinForms Controls in WPF

Gary edited this page Jan 29, 2015 · 2 revisions

You can use WinForms-based component controls and dialogs in a WPF based application. This is made possible by the ControlHostServiceAdapter component, which implements the WinForms IControlHostService so that it works in a WPF environment. In other words, it adapts Sce.Atf.Wpf.Applications.IControlHostService to Sce.Atf.Applications.IControlHostService.

The ControlHostServiceAdapter component exports Sce.Atf.Applications.IControlHostService. If this component is included in an application, the WinForms-based components that need IControlHostService import it from ControlHostServiceAdapter, rather than the WinForms-based ControlHostService component. This allows the components to function with the WPF application. In this case, the application would not include the WinForms-based ControlHostService component — or any other component implementing the WinForms IControlHostService — in its MEF catalog.

ControlHostServiceAdapter also creates an object of the ControlHostClientAdapter class, which adapts to WPF the IControlHostClient interface for WinForms-based control clients. This allows these control clients implementing Sce.Atf.Applications.IControlHostClient to operate properly in WPF.

The StandardInteropParts class includes the ControlHostServiceAdapter component in a MEF TypeCatalog:

public class StandardInteropParts
{
    /// <summary>
    /// Gets type catalog for all components</summary>
    public static ComposablePartCatalog Catalog
    {
        get 
        { 
            return new TypeCatalog(
				typeof(MainWindowAdapter),
				typeof(ContextMenuService),
				typeof(DialogService),
				typeof(ControlHostServiceAdapter)
			);
        }
    }
}

This catalog can be included in an AggregateCatalog, as in this line from the ATF Simple DOM Editor WPF Sample:

return new AggregateCatalog(typeCatalog, StandardInteropParts.Catalog, StandardViewModels.Catalog);

After this, the WPF application can use the WinForms-based component controls — with no additional support from the application.

Topics in this section

Clone this wiki locally