How to use Windows Forms components in a WPF application?

Asked

Viewed 355 times

7

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

I leave a few more questions about the same:

  • There are disadvantages when using WF components in WPF?
  • It is more advantageous to create a WPF component than to use the existing one on the WF?

2 answers

5


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

Yes, provided that it adds references to the following assemblies:

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

Example for the 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 the 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";
        }
    }
}

There are disadvantages when using WF components in WPF?

  • Mixing of technologies, there are some issues to be taken into account, simply because the two technologies do not come together smoothly, others 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.
  • 1

    good response...

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);
}

Need to add

  using System.Windows.Forms;

Source: https://msdn.microsoft.com/pt-br/library/ms751761(v=vs.110). aspx

My practical example

I 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 even, have many examples on the internet, to avoid dependencies with Windows Forms. For example, if you use a styling with Styles in WPF, you will most likely not be able to apply these styles to these guys.

But as I needed to use Reportviewer, I had to appeal to Windowsformshost. However I felt it slowed down this screen.

  • Thanks for the answer. But why should we avoid dependencies with WF?

  • 1

    For example, if you use a styling with Styles in WPF, you will most likely not be able to apply these styles to these guys.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.