WPF中嵌入WinForm中的webbrowser控件
使用VS2008创建WPF应用程序,需使用webbrowser。从工具箱中添加WPF组件中的webbrowser发现其中有很多属性事件不能使用。决定还是使用WinForm中的webbrowser。要想在WPF中使用WinForm控件,查看MSDN,需经过以下步骤。
-
创建名为 HostingWfInWpf 的 WPF 应用程序项目。
-
在解决方案资源管理器中,添加一个对名为 WindowsFormsIntegration.dll 的 WindowsFormsIntegration 程序集的引用。
-
在解决方案资源管理器中,添加一个对名为 System.Windows.Forms.dll 的 Windows 窗体程序集的引用。
-
在 WPF 设计器中打开 Window1.xaml。
-
用以下 XAML 替换 Window1.xaml 中自动生成的 XAML。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <Window x:Class="HostingWfInWpf.Window1" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 Title="HostingWfInWpf" 5 Loaded="WindowLoaded" 6 > 7 <Grid Name="grid1"> 8 9 </Grid> 10 </Window>
6.在代码编辑器中打开 Window1.xaml.cs。
7.将 Window1.xaml.cs 中的代码替换为以下代码。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 using System; 2 using System.Windows; 3 using System.Windows.Controls; 4 using System.Windows.Data; 5 using System.Windows.Documents; 6 using System.Windows.Media; 7 using System.Windows.Shapes; 8 9 using System.Windows.Forms; 10 11 namespace HostingWfInWpf 12 { 13 public partial class Window1 : Window 14 { 15 public Window1() 16 { 17 InitializeComponent(); 18 } 19 20 private void WindowLoaded(object sender, RoutedEventArgs e) 21 { 22 // Create the interop host control. 23 System.Windows.Forms.Integration.WindowsFormsHost host = 24 new System.Windows.Forms.Integration.WindowsFormsHost(); 25 26 // Create the MaskedTextBox control. 27 MaskedTextBox mtbDate = new MaskedTextBox("00/00/0000"); 28 29 // Assign the MaskedTextBox control as the host control's child. 30 host.Child = mtbDate; 31 32 // Add the interop host control to the Grid 33 // control's collection of child controls. 34 this.grid1.Children.Add(host); 35 } 36 } 37 }