允许在 WPF 页面上承载 Windows Forms控件的元素。

命名空间:   System.Windows.Forms.Integration

程序集:   WindowsFormsIntegration(在 WindowsFormsIntegration.dll 中) 用于 XAML 的 XMLNS:http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation

add: http://msdn.microsoft.com/zh-cn/library/system.windows.forms.integration.windowsformshost

如果要引用的话

<Window x:Class="Selection.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="Dates" Height="314" Width="373">     <Grid>     <Label HorizontalAlignment="Left" Margin="20,25,0,0" Name="label1" Width="35.63" Height="23.2766666666667" VerticalAlignment="Top">First</Label>     <Label Height="23.2766666666667" HorizontalAlignment="Left" Margin="20,56,0,0" Name="label2" VerticalAlignment="Top" Width="52.63">Second</Label>     <WindowsFormsHost Name="hostFirst" Margin="85,25,18,0" Height="23.2766666666667" VerticalAlignment="Top">       <wf:DateTimePicker Name="first"/>     </WindowsFormsHost>     <WindowsFormsHost Name="hostSecond" Margin="85,56,18,0" Height="23.2766666666667" VerticalAlignment="Top">       <wf:DateTimePicker Name="second"/>//文本框里是日期     </WindowsFormsHost>     <Button Height="23" HorizontalAlignment="Left" Margin="20,100,0,0" Name="compare" VerticalAlignment="Top" Width="75" Click="compareClick">Compare</Button>     <TextBox Margin="20,131,104,20" Name="info" TextWrapping="WrapWithOverflow" AcceptsReturn="False" IsReadOnly="True" />     <Button Height="22" HorizontalAlignment="Right" Margin="0,0,18,20" Name="quit" VerticalAlignment="Bottom" Width="75" Click="quitClick">Quit</Button>   </Grid> </Window>

后台C#

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.Windows.Forms;

namespace Selection {

/// <summary>

/// Interaction logic for Window1.xaml     /

// </summary>
    public partial class Window1 : Window     {

private DateTimePicker first;

private DateTimePicker second;
        public Window1()         {

InitializeComponent();

//

  Child 获取或设置由 WindowsFormsHost 元素承载的子控件。      

first = hostFirst.Child as DateTimePicker;

second = hostSecond.Child as DateTimePicker;         }
        private void quitClick(object sender, RoutedEventArgs e)         {

this.Close();

}
        private void compareClick(object sender, RoutedEventArgs e)         {

int diff = dateCompare(first.Value, second.Value);

info.Text = "";

show("first == second", diff == 0);

show("first != second", diff != 0);

show("first <  second", diff < 0);

show("first <= second", diff <= 0);

show("first >  second", diff > 0);

show("first >= second", diff >= 0);         }
        private void show(string exp, bool result)         {

info.Text += exp;

info.Text += " : " + result.ToString();

info.Text += "\r\n";

//\r 表示:回车符(ACSII:13 或0x0d),就是我们常说的硬回车。

//  \n 表示:换行(ACSII:10 或0x0a),就是我们常说的软回车。

//\n,好比你在DreamWeaver里做一个网页,在源代码里按一下回车,是给源代码换行。
        }
        private int dateCompare(DateTime leftHandSide, DateTime rightHandSide)         {

// TO DO

return 42;

}

}

}

WPF Windows Forms integration

 

在wpf程序中整合windows form:

1.在references中添加WindowsFormsIntegration和System.Windows.Forms。

2.在xaml中使用的时候要写清楚名字空间,可以把这两个ns定义出来。

下面两句是重点,wpf书写的时候一定要注意引用

xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"

xmlns:wfi="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"

插入WindowsFormsControl:

<wfi:WindowsFormsHost>             <wf:DateTimePicker/> </wfi:WindowsFormsHost>

看到Windows.Forms下面也有Button控件(废话),于是想知道这个Button和WPF通常用的System.Windows.Controls.Button有什么不同。

看了看两者的继承体系:

System.Windows.Controls域中:  Button<ButtonBase<ContentControl<Control

System.Windows.Forms域:         Button<ButtonBase<Control

看似差不多,其实他们完全存在于两个不同的空间,没有一点联系。

两个Button类里定义的内容也不尽相同,Forms.Button看上去内容丰富一些,发现里面有DoubleClick这个event,但是试了试并不起作用,换到windows form 程序下仍然不起作用。(Visual Studio的property界面上看不到这个事件,但是可以在代码里自己加event handler,虽然加了也白加)。Controls.Button里虽然没有DoubleClick,但是MouseDoubleClick是work的。而Forms.Button似乎就只能触发Click事件。

<Window x:Class="WpfApplication1.Window1"     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     Title="Window1" Height="300" Width="300">

<Grid xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"

xmlns:wfi="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"           >

<Grid.RowDefinitions>

<RowDefinition Height="*"/>

<RowDefinition Height="*"/>

</Grid.RowDefinitions>

<Button Grid.Row="1" MouseDoubleClick="Button_MouseDoubleClick">button</Button>

<wfi:WindowsFormsHost Grid.Row="0">

<wf:Button MouseDoubleClick="Button_MouseDoubleClick"/>

</wfi:WindowsFormsHost>     </Grid> </Window>

这两个事件对应的event类型也不一样,一个是System.Windows.Input.MouseButtonEventHandler,一个是System.Windows.Forms.MouseEventHandler,所以对应的event handler的函数的参数也不一样,一个是private void Button_MouseDoubleClick(object sender, MouseButtonEventArgs e),一个是private void Button_MouseDoubleClick(object sender, System.Windows.Forms.MouseEventArgs e),因此两个event handler函数名可以相同。

WPF 中如何使用第三方控件 ,可以使用WindowsFormsHost 类的更多相关文章

  1. WindowsXamlHost:在 WPF 中使用 UWP 的控件(Windows Community Toolkit)

    Windows Community Toolkit 再次更新到 5.0.以前可以在 WPF 中使用有限的 UWP 控件,而现在有了 WindowsXamlHost,则可以使用更多 UWP 原生控件了. ...

  2. 在WPF中添加Windows Form控件(包括 ocx控件)

      首先,需要向项目中的reference添加两个dll,一个是.NET库中的System.Windows.Forms,另外一个是WindowsFormsIntegration,它的位置一般是在C:\ ...

  3. WPF中通过代码设置控件的坐标

    用WPF做贪吃蛇小游戏时,发现了一个问题: 贪吃蛇的移动,我是通过不断刷新Rectangle来实现(贪吃蛇的身体由一组Rectangle组成),因此需要不断调整Rectangle的坐标,但是WPF中没 ...

  4. 在WPF中引用WinForm的控件

     以ArcEngine为例: mapControl = new AxMapControl(); MapHost.Child = mapControl; //MapHost为WindowsFormHos ...

  5. WPF中MVVM模式下控件自有的事件绑定

    1.原因 在WPF中单纯的命令绑定往往不能满足覆盖所有的事件,例如ComboBox的SelectionChanged事件,DataGrid的SelectionChanged事件等等,这时就可以用事件绑 ...

  6. WPF中在后台实现控件样式

    加入现在有一个Button的样式如下: <Style TargetType="{x:Type Button}" x:Key="MyButton">. ...

  7. wpf 中的DataTemplate 绑定控件

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" x ...

  8. WPF第三方控件盘点

    WPF统一的编程模型.语言和框架,实现了界面设计人员和开发人员工作可以分离的境界,鉴于WPF强大的优势,且一直是开发者关注的地方,下面和大家分享基于WPF项目开发需要用到的第三方控件,包括业界最受好评 ...

  9. ActiveReports 报表应用教程 (13)---嵌入第三方控件

    葡萄城ActiveReports报表与Visual Studio完美集成,给我们带来更为灵活易用的报表定制和自定义控件嵌入支持,除了可以在报表中使用葡萄城ActiveReports报表内置控件外,您还 ...

随机推荐

  1. putty 实现不用输入用户名密码直接登陆

    多谢谢Eric的教程 ,下面是我的简化版,原版为Eric所写 远程登陆Linux服务器有两大著名软件,一个是商业软件securecrt,一个是开源软件putty. 两者的安全性能都很高,发展了多年,值 ...

  2. PHP文件操作

    1.递归获取目录下文件的个数 function getFileCount($dir){ if(!is_dir($dir)) return false; //打开目录 $handle = opendir ...

  3. spring heibernate 调用存储过程

    一:参考网址 http://sunbin123.iteye.com/blog/1007556 二:示例 @Autowired @Qualifier("jdbcTemplate") ...

  4. HTML5会砸掉iOS和Android开发者的饭碗么?

    原生App的颠覆 HTML5的“性工能”障碍得到解决,可以接近原生App的效果,所以它就可以替代原生App吗?很多人认为,即使HTML5会发展的比现在好,也将是与原生App各占一部分市场的格局,要求不 ...

  5. PHP中利用PCLZIP压缩解压文件

    <?php include_once('pclzip.lib.php'); $archive = new PclZip('archive.zip'); /* $v_list = $archive ...

  6. Web开发——Http协议剖析

    Http,即超文本传输协议,是建立在TCP/IP协议的基础上.在Web开发的过程中,Http协议是十分重要的,浏览器与服务器之间的交互就是基于Http协议的.Http协议如果展开全面讲解会有很多内容, ...

  7. Python问题之奇怪诡异的Bug

    最近又重新装上了windows 7感觉还是那样,主要是想用M8SDK写些程序.也想在windows上玩玩,一直都觉得用C写一些常用的东东很复杂,只有借助于解释性语言了,在python, ruby间选择 ...

  8. python3 pyqt5 和eric5配置教程

    一.大纲内容: 1.预备PC环境: 2.预备安装程序: 2.1.下载Python3.2 2.2.下载PyQt4 2.3.下载Eric5 3.安装配置步骤: 3.1.安装Pyhon3.2 3.2.安装P ...

  9. 【BZOJ 1040】 [ZJOI2008]骑士

    Description Z国的骑士团是一个很有势力的组织,帮会中汇聚了来自各地的精英.他们劫富济贫,惩恶扬善,受到社会各界的赞扬.最近发生了一件可怕的事情,邪恶的Y国发动了一场针对Z国的侵略战争.战火 ...

  10. MongoDB安装,启动,注册为windows系统服务

    MongoDB安装与启动 周建旭 2014-08-10 解压完后配置环境变量 下载Windows 32-bit或64-bit版本并解压缩,程序文件都在bin目录中,其它两个目录分别是C++调用是的头文 ...