WPF 와 Windows Form Control 간의 상호 운용
왜 이문제를 고민했냐면, WPF 작성한 Application 을 ActiveX Control 로 재작성해야할 필요성이 생겼다. 근데 WPF 는 ActiveX Control 을 작성하지 못하는 것 같았고(?), Windows Form Control 은 ActiveX Control 을 작성하는 방법을 이미 알고 있었다. 그래서 WPF 로 작성한 Application 을 Control 로 조금 수정하고, 이를 Windows Form 형태의 Proxy Control (WPF Control 을 hosting 함)을 만들어서 ActiveX 컨트롤로 작성하기 위해서였다. 약간 트릭이긴 하지만... 그래도 이 방법이 시간과 비용을 절약하는 최선의 방법인것 같았다. |
Walkthrough: Hosting a Windows Forms Control in WPF
(http://msdn.microsoft.com/en-us/library/ms751761.aspx)
To host the MaskedTextBox control
1.Create a WPF Application project named HostingWfInWpf.
2.In Solution Explorer, add a reference to the WindowsFormsIntegration assembly, which is named WindowsFormsIntegration.dll.
3.In Solution Explorer, add a reference to the Windows Forms assembly, which is named System.Windows.Forms.dll.
4.Open Window1.xaml in the WPF Designer.
5.Replace the automatically generated XAML in Window1.xaml with the following XAML.
<Window x:Class="HostingWfInWpf.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="HostingWfInWpf" Loaded="WindowLoaded" > <Grid Name="grid1"> </Grid> </Window> |
6.In the Code Editor, open Window1.xaml.cs.
7.Replace the code in Window1.xaml.cs with the following code.
using System; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Media; using System.Windows.Shapes; using System.Windows.Forms; namespace HostingWfInWpf { public partial class Window1 : Window { public Window1() { InitializeComponent(); } private void WindowLoaded(object sender, RoutedEventArgs e) { // Create the interop host control. System.Windows.Forms.Integration.WindowsFormsHost host = new System.Windows.Forms.Integration.WindowsFormsHost(); // Create the MaskedTextBox control. MaskedTextBox mtbDate = new MaskedTextBox("00/00/0000"); // Assign the MaskedTextBox control as the host control's child. host.Child = mtbDate; // Add the interop host control to the Grid // control's collection of child controls. this.grid1.Children.Add(host); } } } |
ElementHost Class
(http://msdn.microsoft.com/en-us/library/system.windows.forms.integration.elementhost.aspx)
A Windows Forms control that can be used to host a Windows Presentation Foundation (WPF) element.
Use the ElementHost control to place a WPF UIElement on your Windows Forms control or form. To host a Windows Forms control in a WPF element, use the WindowsFormsHost element.
|
To host a WPF element in a Windows Form, you must assign the WPF element to the Child property.
Use the PropertyMap property to assign custom mappings between an ElementHost control and its hosted WPF element. For more information, see Windows Forms and WPF Property Mapping.
private void Form1_Load(object sender, EventArgs e) { // Create the ElementHost control for hosting the // WPF UserControl. ElementHost host = new ElementHost(); host.Dock = DockStyle.Fill; // Create the WPF UserControl. HostingWpfUserControlInWf.UserControl1 uc = new HostingWpfUserControlInWf.UserControl1(); // Assign the WPF UserControl to the ElementHost control's // Child property. host.Child = uc; // Add the ElementHost control to the form's // collection of child controls. this.Controls.Add(host); } |
참고: Walkthrough: Hosting a WPF Control in Windows Forms (내용이 좀 길다)
(http://msdn.microsoft.com/en-us/library/ms742215.aspx)