How to use Windows Forms components in a WPF application?

7

Is there any way to use Windows Forms components in WPF? For example, do I need to use the control Chart or MaskedTextBox of Windows Forms, which does not exist in WPF applications, how could I use it in my WPF application?

I leave some more questions about it:

  • Are there disadvantages when using WF components in WPF?
  • It is more advantageous to create a WPF component than to use the existing one in WF?
asked by anonymous 24.09.2016 / 03:47

2 answers

5
  

Is there any way to use Windows Forms components in WPF?

Yes , as long as you add references to the project to the following assemblies :

To use them use a WindowsFormsHost and add the components as children.

Example for MaskedTextBox :

<Window x:Class="TesteWpf.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <WindowsFormsHost>
            <wf:MaskedTextBox Mask="00/00/0000">

            </wf:MaskedTextBox>

        </WindowsFormsHost>

    </Grid>
</Window>

Example for Chart :

Add a reference to assembly System.Windows.Forms.DataVisualization

MainWindow.xaml

<Window x:Class="TesteWpf.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:winformchart="clr-namespace:System.Windows.Forms.DataVisualization.Charting;assembly=System.Windows.Forms.DataVisualization"
        Title="MainWindow" Height="350" Width="525">
    <Grid>

        <WindowsFormsHost>
            <winformchart:Chart x:Name="Chart" Dock="Fill">
                <winformchart:Chart.Series>
                    <winformchart:Series Name="series" ChartType="Line"/>
                </winformchart:Chart.Series>
                <winformchart:Chart.ChartAreas>
                    <winformchart:ChartArea/>
                </winformchart:Chart.ChartAreas>
            </winformchart:Chart>
        </WindowsFormsHost>

    </Grid>
</Window>

MainWindow.xaml.cs

using System.Collections.Generic;
using System.Windows;
using System.Windows.Forms.DataVisualization.Charting;

namespace TesteWpf
{
    public partial class MainWindow : Window
    {

        public MainWindow()
        {
            InitializeComponent();
            var graphicValues = new Dictionary<int, double>();
            for (int i = 0; i < 10; i++)
            {
                graphicValues.Add(i, 10 * i);
            }

            Chart.DataSource = graphicValues;
            Chart.Series["series"].XValueMember = "Key";
            Chart.Series["series"].YValueMembers = "Value";
        }
    }
}
  

Are there disadvantages when using WF components in WPF?

  • Mixing of technologies, there are some issues to consider , one simply because the two technologies do not come together smoothly, other because they are either design decisions or design differences between Windows Forms and WPF.
  

It is more advantageous to create a WPF component than to use the existing one in WF.

  • For the above reasons.
  • If you have the availability and ingenuity to create it.
26.09.2016 / 15:33
2

XAML

<Grid Name="grid1">

</Grid>

Code Behind

private void Window_Loaded(object sender, RoutedEventArgs e) 
{
    // Cria um host para windows forms
    System.Windows.Forms.Integration.WindowsFormsHost host =
        new System.Windows.Forms.Integration.WindowsFormsHost();

    // cria o componente de máscara do windows forms
    MaskedTextBox mtbDate = new MaskedTextBox("00/00/0000");

    // adiciona o MaskedTextBox no host
    host.Child = mtbDate;

    // adiciona o host na grid do WPF
    this.grid1.Children.Add(host);
}

You need to add

  using System.Windows.Forms;

Source : link

My practice example

I have already used Windows Forms Host to use ReportViewer in WPF

<mah:MetroWindow x:Class="Capacitor.WPF.View.Relatorios.TelaRelatorio"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:mah="http://metro.mahapps.com/winfx/xaml/controls"
        xmlns:view="clr-namespace:Capacitor.WPF.View"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:rv="clr-namespace:Microsoft.Reporting.WinForms;assembly=Microsoft.ReportViewer.WinForms"
        BorderBrush="{DynamicResource AccentColorBrush}"
        Title="Relatório" Height="1101.88" Width="814.286" WindowState="Maximized">
    <Grid>
        <WindowsFormsHost>
            <rv:ReportViewer x:Name="reportViewer"/>
        </WindowsFormsHost>
    </Grid>
</mah:MetroWindow>

I recommend creating a mask controlle in WPF same, there are many examples on the internet, to avoid dependencies with Windows Forms. For example, if you use styling with Styles in WPF, you probably will not be able to apply those styles to those faces.

But since I needed to use ReportViewer, I had to call WindowsFormsHost. But I felt it slowed down.

    
26.09.2016 / 15:29