彩票走势图

如何在WPF中內嵌WindowsForm控制项

转帖|其它|编辑:郝浩|2011-01-04 13:59:30.000|阅读 1251 次

概述:在开发WPF的专案时,有时候可能会碍于平台的限制或是使用的语言及类别库的限制,部份功能还是得透过Windows Form来或是某些Windows Form控制项来实作,可是整个系统的外观已经是既定的WPF了,那要怎么把Windows Form整合进来呢?本文将介绍如何在WPF中內嵌WindowsForm控制项,希望对大家有所帮助。

# 慧都年终大促·界面/图表报表/文档/IDE等千款热门软控件火热促销中 >>

  在开发WPF的专案时,有时候可能会碍于平台的限制或是使用的语言及类别库的限制,部份功能还是得透过Windows Form来或是某些Windows Form控制项来实作,可是整个系统的外观已经是既定的WPF了,那要怎么把Windows Form整合进来呢??

  首先,得引用WindowsFormsIntegration.dll(若使用.Net Framework 4.0版的话,预设路径在C:\Windows\Microsoft.NET\Framework\v4.0.30319\WPF),以及System.Windows.Forms,接着就可以在WPF中透过WindowsFormsHost这个元件来承载Windows Form的控制项了!!

  来写个小小的範例:

  MainWindow.xaml

1: <Window x:Class="WPF_Winform.MainWindow"
2: xmlns="//schemas.microsoft.com/winfx/2006/xaml/presentation"
3: xmlns:x="//schemas.microsoft.com/winfx/2006/xaml"
4: xmlns:mc="//schemas.openxmlformats.org/markup-compatibility/2006"
5: xmlns:d="//schemas.microsoft.com/expression/blend/2008"
6: xmlns:WinFormHost="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
7: xmlns:WinFormControls="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
8: mc:Ignorable="d"
9: Title="MainWindow"
10: d:DesignWidth="800" d:DesignHeight="600"
11: Width="Auto" Height="Auto">
12: <Grid>
13: <WinFormHost:WindowsFormsHost Name="winFormHost">
14: <WinFormControls:FlowLayoutPanel Name="floPanel" BackColor="Transparent">
15: <WinFormControls:FlowLayoutPanel.Controls>
16: <WinFormControls:Label Text="WinformLabel"/>
17: <WinFormControls:TextBox Width="200"/>
18: <WinFormControls:Button Text="WinformButton" Width="150"/>
19: </WinFormControls:FlowLayoutPanel.Controls>
20: </WinFormControls:FlowLayoutPanel>
21: </WinFormHost:WindowsFormsHost>
22: </Grid>
23: </Window>

  那~能不能在这边的Windows Form控制项裡面再放WPF控制项呢?答案是可以的!!透过ElementHost控制项就可以办到啰,而且我们也可以直接在XAML中分别替Windows Form和WPF的按钮元件加入Click的EventHandler喔!!

我们来修改一下MainWindow.xaml

1: <Window x:Class="WPF_Winform.MainWindow"
2: xmlns="//schemas.microsoft.com/winfx/2006/xaml/presentation"
3: xmlns:x="//schemas.microsoft.com/winfx/2006/xaml"
4: xmlns:mc="//schemas.openxmlformats.org/markup-compatibility/2006"
5: xmlns:d="//schemas.microsoft.com/expression/blend/2008"
6: xmlns:WinFormHost="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
7: xmlns:WinFormControls="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
8: mc:Ignorable="d"
9: Title="MainWindow"
10: d:DesignWidth="800" d:DesignHeight="600"
11: Width="Auto" Height="Auto">
12: <Grid Background="Gray">
13: <WinFormHost:WindowsFormsHost Name="winFormHost" VerticalAlignment="Center" HorizontalAlignment="Center" Background="White">
14: <WinFormControls:FlowLayoutPanel Name="floPanel" BackColor="Transparent">
15: <WinFormControls:FlowLayoutPanel.Controls>
16: <WinFormControls:Label Text="WinformLabel"/>
17: <WinFormControls:TextBox Width="200"/>
18: <WinFormControls:Button Text="WinformButton" Width="150" Click="Button_Click"/>
19: <ElementHost Height="200" Width="200">
20: <Grid Height="200" Width="200" VerticalAlignment="Center" HorizontalAlignment="Center" Background="Yellow">
21: <Button Content="WPF Button" Width="150" Height="40" Click="Button_Click"/>
22: </Grid>
23: </ElementHost>
24: </WinFormControls:FlowLayoutPanel.Controls>
25: </WinFormControls:FlowLayoutPanel>
26: </WinFormHost:WindowsFormsHost>
27: </Grid>

还有MainWindow.xaml.cs,Windows Form和WPF的Button产生出来的Click EventHandler长相不一样喔!!

1: using System.Windows;
2:
3: namespace WPF_Winform
4: {
5: public partial class MainWindow : Window
6: {
7: public MainWindow()
8: {
9: InitializeComponent();
10: }
11:
12: private void Button_Click( object sender , System.EventArgs e )
13: {
14: MessageBox.Show("你點了Winform的按鈕");
15: }
16:
17: private void Button_Click( object sender , RoutedEventArgs e )
18: {
19: MessageBox.Show( "你點了WPF的按鈕" );
20: }
21: }
22: }  

执行结果会变这样:

 

  不过要注意一点,尤于Render机制不同的关係,若是想要把WPF的控制项覆盖在WindowsFormsHost控制项之上的话,是做不到的喔!!WindowsFormsHost塬生就会具有类似AlwaysOnTop的效果。


 


标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@cahobeh.cn

文章转载自:网络转载

为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP