يمكن استخدام تحكمات Windows Forms من داخل تطبيق WPF من خلال استخدام الفئة WindowsFormsHost Class والتي يكون تعريفها ضمن Visual Basic كالتالي
<ContentPropertyAttribute("Child")> _
Public Class WindowsFormsHost _
Inherits HwndHost _
Implements IKeyboardInputSinkواستخدامها
Dim instance As WindowsFormsHost
وهي موجودة ضمن مجال الأسماء System.Windows.Forms.Integration وفي المجمع WindowsFormsIntegration وفي المكتبة WindowsFormsIntegration.dll حيث يبين لنا المثال التالي كيفية استخدام العنصر WindowsFormsHost لاستضافة تحكمات Windows Forms
<Window x:Class="HostingWfInWpf.Window1" 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="HostingWfInWpf" > <Grid> <WindowsFormsHost> <wf:MaskedTextBox x:Name="mtbDate" Mask="00/00/0000"/> </WindowsFormsHost> </Grid> </Window>
<Window x:Class="Window1" 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="HostingWfInWpf" > <Grid> <WindowsFormsHost> <wf:MaskedTextBox x:Name="mtbDate" Mask="00/00/0000"/> </WindowsFormsHost> </Grid> </Window>
ولاستضافة تحكم Windows forms من ضمن تطبيق WPF يجب عليك تحديد تحكم Windows Forms إلى الخاصية Child وذلك باستخدام الخاصية PropertyMap لتحديد الربط بين العنصر WindowsFormsHost و تحكم Windows Forms المستضاف ضمنه
أنشئ تطبيقا من النوع WPF Application و سمه HostingWfInWpf وفي Solution Explorer أضف مرجعا للمجمع WindowsFormsIntegration والمسمى WindowsFormsIntegration.dll ثم قم بفتح Window1.xaml واستبدل كود 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>
<Window x:Class="Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="HostingWfInWpf" Height="300" Width="300" Loaded="WindowLoaded" > <Grid Name="grid1"> </Grid> </Window>
افتح الملف Window1.xaml.vb في محرر الكود وقم باستبدال جميع الكود الموجود بالكود التالي
Imports System
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Data
Imports System.Windows.Documents
Imports System.Windows.Media
Imports System.Windows.Media.Imaging
Imports System.Windows.Shapes
Imports System.Windows.Forms
' Interaction logic for Window1.xaml
Partial Public Class Window1
Inherits Window
Public Sub New()
InitializeComponent()
End Sub
Private Sub WindowLoaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
' Create the interop host control.
Dim host As New System.Windows.Forms.Integration.WindowsFormsHost()
' Create the MaskedTextBox control.
Dim mtbDate As 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.
Me.grid1.Children.Add(host)
End Sub 'WindowLoaded
End Classاضغط F5 لاختبار المشروع