WPF 嵌入winform 控件

时间:2023-03-09 06:45:15
WPF 嵌入winform 控件

引入 WindowsFormsIntegration.dll   和   System.Windows.Forms.dll

  

<Window x:Class="wgscd.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:WinFormHost="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
xmlns:WinFormControls="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms" Title="Window1" Height="362" Width="655">
<Grid Name="grid">
<WindowsFormsHost Margin="0,65,0,0" Name="windowsFormsHost1" >
<WinFormControls:WebBrowser x:Name="web"/>
</WindowsFormsHost>
<Button Height="39" VerticalAlignment="Top" HorizontalAlignment="Left" Width="159" Margin="107,20,0,0" Click="Button_Click">gggg</Button>
</Grid>
</Window>

  

也可以代码动态添加:

System.Windows.Forms.Integration.WindowsFormsHost host =
new System.Windows.Forms.Integration.WindowsFormsHost(); System.Windows.Forms.WebBrowser web = new System.Windows.Forms.WebBrowser(); web.Url = new Uri("http://www.baidu.com"); host.Child = web; this.grid.Children.Add(host);