はじめに
この記事ではWPFアプリケーションでWindowsフォームのコントロールを使用する手順について確認します。
Windowsフォームのコントロールを使用するには、参照設定に以下の2つのアセンブリを追加しておく必要があります。
- System.Windows.Forms
- WindowsFormsIntegration
プロジェクトのファイル構成は下図のとおりです。

開発環境は以下のとおりです。
オペレーティングシステム | Windows10 x64 |
Visual Studio | Microsoft Visual Studio Community 2019 Version 16.11.2 |
.NET Framework | 4.7.2 |
サンプルプログラムの動作仕様
WPFアプリケーションに以下のコントロールを表示することにします。
- PropertyGrid(Windowsフォーム)
- DataGridView(Windowsフォーム)
- ProgressBar(Windowsフォーム)
- ProgressBar(WPF)
XAMLファイルの内容
XAMLファイルは以下のとおりです。
<Window x:Class="WpfUsingWinForm.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfUsingWinForm"
xmlns:forms="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
mc:Ignorable="d"
Title="MainWindow" Height="600" Width="400">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<Button x:Name="wpfButton" Content="検証" Width="60" Height="30"/>
<WindowsFormsHost Grid.Column="1" Width="300">
<forms:PropertyGrid x:Name="frmPropertyGrid"/>
</WindowsFormsHost>
<WindowsFormsHost Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2">
<forms:DataGridView x:Name="frmDataGridView"
ColumnCount="3"
RowCount="3"
ColumnHeadersHeight="30"/>
</WindowsFormsHost>
<WindowsFormsHost Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2">
<forms:ProgressBar x:Name="frmProgressBar"
Maximum="60"/>
</WindowsFormsHost>
<ProgressBar x:Name="wpfProgressBar"
Grid.Row="3"
Grid.Column="0"
Grid.ColumnSpan="2"
Maximum="60"/>
</Grid>
</Window>
Windowsフォームのコントロールを参照できるようにするため、以下のXMLNS宣言を追加します。
xmlns:forms="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
これでWindowsFormsHost要素の中に必要なWindowsフォームのコントロールを追加できます。
コードビハインドの内容
コードビハインドは以下のとおりです。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
namespace WpfUsingWinForm
{
/// <summary>
/// MainWindow.xaml の相互作用ロジック
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
frmPropertyGrid.SelectedObject = wpfButton;
frmDataGridView.Columns[0].HeaderText = "名前";
frmDataGridView.Columns[1].HeaderText = "年齢";
frmDataGridView.Columns[2].HeaderText = "性別";
frmDataGridView.Rows[0].Cells[0].Value = "山田太郎";
frmDataGridView.Rows[0].Cells[1].Value = 20;
frmDataGridView.Rows[0].Cells[2].Value = "男性";
frmDataGridView.Rows[1].Cells[0].Value = "山田花子";
frmDataGridView.Rows[1].Cells[1].Value = 25;
frmDataGridView.Rows[1].Cells[2].Value = "女性";
frmDataGridView.Rows[2].Cells[0].Value = "高橋一郎";
frmDataGridView.Rows[2].Cells[1].Value = 30;
frmDataGridView.Rows[2].Cells[2].Value = "男性";
timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromMilliseconds(200);
timer.Tick += OnTimerTick;
timer.Start();
}
private DispatcherTimer timer = null;
private int n = 0;
private void OnTimerTick(object sender, EventArgs e)
{
if(n <= frmProgressBar.Maximum)
{
frmProgressBar.Value = n;
}
if(n <= wpfProgressBar.Maximum)
{
wpfProgressBar.Value = n;
}
n++;
}
}
}
PropertyGridコントロールにはWPFのButtonコントロールを設定します。
DataGridViewにデータを追加します。
WPFのDispatcherTimerクラスを使って、WindowsフォームとWPFそれぞれのProgressBarを進めています。
ビルドして実行すると以下のウィンドウが表示されます。

今回はWPFアプリケーションでWindowsフォームのコントロールを使用する手順を確認しました。
以上です。
コメント